Page 1 of 1
modding: player.character throws an exception
Posted: Tue Sep 19, 2017 8:51 am
by mrvn
From the API docs player.character is supposed to return nil when a player is offline. But it seems to throw an exception instead.
This causes the bluebuild2 mod to fail when it runs
Wrapping this in a pcall() fixes the crash but it's not what the API docs say.
Re: modding: player.character throws an exception
Posted: Tue Sep 19, 2017 11:16 am
by posila
Thanks for the report. Can you link to part of documentation that says it returns nil when a player is offline, so we can fix it?
Current
LuaPlayer::character documentation says this
character :: LuaEntity [Read-Write]
The character attached to this player, or nil if no character.
Note: It is not valid to access this attribute when the player is disconnected (see LuaPlayer::connected).
Re: modding: player.character throws an exception
Posted: Tue Sep 19, 2017 2:30 pm
by wheybags
Ok, so we decided to go ahead and have it return nil in this case.
This behaviour will be in 0.16
Re: modding: player.character throws an exception
Posted: Tue Sep 19, 2017 2:55 pm
by mrvn
I interpreted the "or nil if no character" to cover the case of the player being offline and "access" to mean that if the player is not connected you will access a nil value and that will naturally fail when you try to do anything with it.
But I guess I will patch in a check for player.connected instead of the pcall() for 0.15.
Re: modding: player.character throws an exception
Posted: Tue Sep 19, 2017 3:53 pm
by Bilka
May I ask how you come to try to access this offline player? If it is because you are looping through game.players, you can loop through game.connected_players instead, which saves you the extra check.
Re: modding: player.character throws an exception
Posted: Thu Sep 21, 2017 9:32 am
by mrvn
I'm not exactly sure. At first I just had bluebuild2 crash. After loading the last autosave it worked again for a short time and crashed again. Loading again and everyone present turning off bluebuild2 it still crashed. After looking at the code I think I know what is happening.
I think it is a combination of one player having left, the autodeconstruct mod and bluebuild2. The missing player must be standing near an electric miner. So when the miner runs out it gets marked for deconstruction and then bluebuild2 checks the player character to see if it should deconstruct it.