Page 1 of 1

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

Posted: Tue Jul 05, 2016 3:12 am
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.

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

Posted: Tue Jul 05, 2016 8:43 am
by prg
character.valid won't do anything useful if character is already nil, so check player.character first?

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

Posted: Tue Jul 05, 2016 11:25 am
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).

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

Posted: Tue Jul 05, 2016 11:32 pm
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.

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

Posted: Wed Jul 06, 2016 8:07 am
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

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

Posted: Wed Jul 06, 2016 9:03 am
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.

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

Posted: Wed Jul 06, 2016 9:12 am
by binbinhfr
yes sorry, I forgot this one. Edited.

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

Posted: Wed Jul 06, 2016 6:18 pm
by TheSAguy
Thanks appreciate the feedback!