attempt to index field 'character' (a nil value) - Help Please

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

attempt to index field 'character' (a nil value) - Help Please

Post by TheSAguy »

Hi,

I've got a report with the below error in MP. This was also reported back in 0.12, so not new to 0.13.

Code: Select all

10714.937 Error MultiplayerManager.cpp:129: MultiplayerManager failed: "Error while running the event handler: __Natural_Evolution_Enemies__/control.lua:201: attempt to index field 'character' (a nil value)"
Below is my code. I'm already checking if "character" is valid. So not sure what else to do.

Code: Select all

			for i = 1, #game.players, 1 do
			player = game.players[i]
         
				if player.connected and player.character.valid then
					player.surface.set_multi_command{command = {type=defines.command.attack, target=player.character, distraction=defines.distraction.by_enemy},unit_count = (20+math.floor(game.evolution_factor*100/#game.players)), unit_search_distance = 600}
				end
			end		
Line 201 is: "if player.connected and player.character.valid then"

Any suggestions on a fix?
Thanks.
Attachments
control.lua
control.lua
(11.77 KiB) Downloaded 122 times
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: attempt to index field 'character' (a nil value) - Help Please

Post by prg »

character.valid won't do anything useful if character is already nil, so check player.character first?
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: attempt to index field 'character' (a nil value) - Help Please

Post by binbinhfr »

note that if player.character of a player is nil, it means that this player is in god mode (no little man associated on the map).
My mods on the Factorio Mod Portal :geek:
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: attempt to index field 'character' (a nil value) - Help Please

Post by TheSAguy »

Does anyone see any issue with how I fixed this? It works in SP:

Code: Select all

				for i = 1, #game.players, 1 do
				player = game.players[i]
			 
					if player then
					if player.valid then
					if player.character.valid then
					if player.connected then
						player.surface.set_multi_command{command = {type=defines.command.attack, target=player.character, distraction=defines.distraction.by_enemy},unit_count = math.floor(Enemy_Count * game.evolution_factor), unit_search_distance = 1500}
					end
					end
					end
					end
				end
Thanks.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: attempt to index field 'character' (a nil value) - Help Please

Post by binbinhfr »

no need to imbricate 4 tests, as AND tests are stopped at first false result.
No need to test player : if in game.players, it should not be nil.

Despite of this, I would do it in that order :
for _, player in pairs(game.players) do
if player.connected and player.valid and player.character and player.character.valid then
Last edited by binbinhfr on Wed Jul 06, 2016 9:11 am, edited 1 time in total.
My mods on the Factorio Mod Portal :geek:
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: attempt to index field 'character' (a nil value) - Help Please

Post by prg »

binbinhfr wrote:Despite of this, I would do it in that order :
for _, player in pairs(game.players) do
if player.connected and player.valid and player.character.valid then
That'll still blow up if player.character is nil. You need to check for the existance of player.character itself before checking for player.character.valid.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: attempt to index field 'character' (a nil value) - Help Please

Post by binbinhfr »

yes sorry, I forgot this one. Edited.
My mods on the Factorio Mod Portal :geek:
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: attempt to index field 'character' (a nil value) - Help Please

Post by TheSAguy »

Thanks appreciate the feedback!
Post Reply

Return to “Modding help”