modding: player.character throws an exception

This subforum contains all the issues which we already resolved.
Post Reply
mrvn
Smart Inserter
Smart Inserter
Posts: 5704
Joined: Mon Sep 05, 2016 9:10 am
Contact:

modding: player.character throws an exception

Post 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

Code: Select all

if player.character and .....
Wrapping this in a pcall() fixes the crash but it's not what the API docs say.

posila
Factorio Staff
Factorio Staff
Posts: 5202
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: modding: player.character throws an exception

Post 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).

User avatar
wheybags
Former Staff
Former Staff
Posts: 328
Joined: Fri Jun 02, 2017 1:50 pm
Contact:

Re: modding: player.character throws an exception

Post by wheybags »

Ok, so we decided to go ahead and have it return nil in this case.
This behaviour will be in 0.16

mrvn
Smart Inserter
Smart Inserter
Posts: 5704
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: modding: player.character throws an exception

Post 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.

Bilka
Factorio Staff
Factorio Staff
Posts: 3132
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: modding: player.character throws an exception

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

mrvn
Smart Inserter
Smart Inserter
Posts: 5704
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: modding: player.character throws an exception

Post 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.

Post Reply

Return to “Resolved Problems and Bugs”