Events don't document when player_index is nil

Place to report issues and suggest improvements to the API documentation.
Post Reply
User avatar
Muppet9010
Filter Inserter
Filter Inserter
Posts: 278
Joined: Sat Dec 09, 2017 6:01 pm
Contact:

Events don't document when player_index is nil

Post by Muppet9010 »

A lot of the events are missing an explanation for when the `player_index` will be empty. As for some it relates to how a script raised the event and what options it passed in (which may not always be apparent).
i.e. https://lua-api.factorio.com/latest/eve ... nstruction
on the `LuaEntity.order_deconstruction()` if the optional `player` field is left blank then no `player_index` appears in the event data. If the optional `player` field is populated or a player did the marking for deconstruction in-game then the the `player_index` event data field is populated.

Honktown
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Small documentation improvement requests

Post by Honktown »

Muppet9010 wrote:
Tue Jul 12, 2022 2:07 am
A lot of the events are missing an explanation for when the `player_index` will be empty. As for some it relates to how a script raised the event and what options it passed in (which may not always be apparent).
i.e. https://lua-api.factorio.com/latest/eve ... nstruction
on the `LuaEntity.order_deconstruction()` if the optional `player` field is left blank then no `player_index` appears in the event data. If the optional `player` field is populated or a player did the marking for deconstruction in-game then the the `player_index` event data field is populated.
The basic rules for events are 1) the game will never raise an event with invalid LuaObjects and 2) (or an extension to 1), if a mod causes optional parameters to become invalid, this information won't be passed on to later mods which listen to the event.

It's not meaningful to describe *how* an optional parameter isn't provided. Technically, there are two/three cases. Always. 1+2) The optional parameter was not a part of the event being raised. This is "1+2" because it applies if the action from the game did not have the optional element, and whether it was not provided as part of script code call. The other/ 3) case, is when something becomes invalid while an event is being processed by another mod. Events are delivered to mods in loaded order, and a mod can cause an optional parameter to become invalid (which causes that parameter to be nil for later mods), or if the parameter is not optional (e.g. entity for on_built_entity), the later mods do not receive the event at all.

I tested non-optional player_indexes, and unless I am mistaken, a player can only be removed by game.remove_offline_players(). A player is not "offline" until a tick after being kicked it seems. Kicking/banning a player and removing offline players immediately did not trigger player removal. (tested it to check as that's one of the only values that could become "meaningless", because it is about the only value which could mean nothing to another mod: game.get_player(player_index) would fail if a player were removed, but a player_index is not a LuaObject so it doesn't become invalid) [Edit: By "fail" I mean return a nil player, "not work as desired". The game is perfectly fine returning nil for players that don't exist within the number range]
I have mods! I guess!
Link

Post Reply

Return to “Documentation Improvement Requests”