Rseding91 wrote: Sun Nov 04, 2018 4:35 pm
Then the documentation is incorrect. The event is fired when the thing that owns the entity selector changes what it has selected when it has an associated player.
For character entities that's the character. For the god controller that's the god controller. If a character has no player associated then it won't fire the event. If you change who owns a character the selected entity for that character hasn't changed but the owning player has.
That's just how this event works.
Okay, let me see if I understand correctly:
1. An "entity selector" is owned by a controller (character or god controller), not by a player.
2. on_selected_entity_changed is generated when a controller's selection on tick N is different from its selection on tick N-1, and only if the controller has an associated player on both ticks N and N-1.
3. In my use case, on tick N there is a god controller G associated with player P, and on tick N-1 there was a character controller C associated with player P. Since neither G nor C had an associated player on both ticks N and N-1, no event is generated.
4. The surface change is completely unrelated. If the cursor position changes such that a different entity is selected, and this happens on the same tick that a player's controller changes (from character to god controller, or vice versa, or from character to another character), then this is also a scenario where on_selected_entity_changed would
not be fired.
Do I have that all correct? This is all pretty counter-intuitive, since from a player's perspective selection is a UI concept tied to the mouse cursor, and is logically assumed to be owned by a connected Player (a disconnected player doesn't have a mouse cursor and can't select anything).
So let's test. Without any change of controller, let's clear the selection. As expected, the selection is restored on the next tick by the UI, and we get two on_selected_entity_changed events:
Code: Select all
799.301 Script script.on_event(defines.events.on_selected_entity_changed, function(e) last_entity_name=e.last_entity and e.last_entity.name or "(none)" s=game.players[e.player_index].selected e.current_selection=s and s.name or "(none)" e.last_entity_name=last_entity_name log(serpent.block(e)) end ):1: {
current_selection = "(none)",
last_entity = {
__self = "userdata: 0x0000019ae7baa460"
},
last_entity_name = "stone-furnace",
name = 52,
player_index = 1,
tick = 39698
}
799.301 Script @__creative-mode-fix__/scripts/events.lua:1041: T39698 on_console_command (ID 72)
T39698 (ID 72) "player_index" :: uint: 1
T39698 (ID 72) "command" :: string: "c"
T39698 (ID 72) "parameters" :: string: "game.player.clear_selected_entity()"
799.317 Script script.on_event(defines.events.on_selected_entity_changed, function(e) last_entity_name=e.last_entity and e.last_entity.name or "(none)" s=game.players[e.player_index].selected e.current_selection=s and s.name or "(none)" e.last_entity_name=last_entity_name log(serpent.block(e)) end ):1: {
current_selection = "stone-furnace",
last_entity_name = "(none)",
name = 52,
player_index = 1,
tick = 39699
}
Now with changing from a character to a god controller, but without changing player position, cursor position, or selection. This is a scenario where I would
not expect on_selected_entity_changed to be fired, but it is:
Code: Select all
1024.850 Script @__creative-mode-fix__/scripts/events.lua:1041: T53231 on_console_command (ID 72)
T53231 (ID 72) "player_index" :: uint: 1
T53231 (ID 72) "command" :: string: "c"
T53231 (ID 72) "parameters" :: string: "game.player.character=nil"
1024.867 Script script.on_event(defines.events.on_selected_entity_changed, function(e) last_entity_name=e.last_entity and e.last_entity.name or "(none)" s=game.players[e.player_index].selected e.current_selection=s and s.name or "(none)" e.last_entity_name=last_entity_name log(serpent.block(e)) end ):1: {
current_selection = "stone-furnace",
last_entity_name = "(none)",
name = 52,
player_index = 1,
tick = 53232
}
If I understand correctly, what's happening here is that the character is disassociated from the player, which also causes it to lose its selection, but since it now isn't associated with a player, no event is fired. The player is now associated with a god controller, which doesn't have any selection. On the next tick, the UI updates the controller's entity selector based on the cursor position, which causes on_selected_entity_changed to be fired.
If I then move the mouse cursor such that no entity is selected, I get on_selected_entity_changed again, which is expected. However, if I then reassociate with the old character entity (/c game.player.character=old_character), that character's entity selector is still recording that the stone-furnace is selected, and on the next tick I get another on_selected_entity_changed event, with last_entity set to the stone-furnace, even though from the perspective of the player's UI, that stone-furnace hasn't been selected for a long time!