Relation between on_player_died and on_post_entity_died

Place to get help with not working mods / modding interface.
User avatar
UnseenFactory
Manual Inserter
Manual Inserter
Posts: 4
Joined: Tue Oct 29, 2019 5:41 am
Contact:

Relation between on_player_died and on_post_entity_died

Post by UnseenFactory »

For the mod I'm writing I like to use all the data in on_post_entity_died for when the player/character dies.
The only problem is that there is no way to get the player who died. I know that the corpse in the event contains character_corpse_player_index but what if there is a mod installed that prevents the player from dropping a corpse. (I like to think of every edge case ;) )

My next solution was to also listen to on_player_died and locally store the player index and tick. Then when on_post_entity_died fires I can match the tick and add the data to complete the process. But for this I need to be sure that both events trigger on the same tick, is this the case? And can it ever happen that on_player_died fires, but on_post_entity_died doesn't?

Another edge case I'm thinking of is when 2 players die at the exact same time. What is the order of events firing? Is the order like this:
on_player_died(player 1)
on_player_died(player 2)
on_post_entity_died(player 1)
on_post_entity_died(player 2)

Or like this:
on_player_died(player 1)
on_post_entity_died(player 1)
on_player_died(player 2)
on_post_entity_died(player 2)

I hope and assume the latter. I don't really have a way to test this myself, so I would love some info here. :D
User avatar
UnseenFactory
Manual Inserter
Manual Inserter
Posts: 4
Joined: Tue Oct 29, 2019 5:41 am
Contact:

Re: Relation between on_player_died and on_post_entity_died

Post by UnseenFactory »

I decided to do some testing and got all the answers I needed :D

I killed all enemies at the same time with the console command from the wiki, but using entity.die() instead of destroy(). https://wiki.factorio.com/Console#Kill_all_enemies
That way the events on_entity_died and on_post_entity_died are triggered. So I wrote subscribers to those events that log the event id, tick, and unit_number. That way I found out that the game processes all events of an entity before moving on to the next one. To illustrate:
on_entity_died(enemy #1)
on_post_entity_died(enemy #1)
on_entity_died(enemy #2)
on_post_entity_died(enemy #2)
on_entity_died(enemy #3)
on_post_entity_died(enemy #3)

Also all the events were triggered in the exact same tick, except for the worms, they always died exactly 41 ticks later. That is because they first need to show the animation of coming out of their hole and then they die.

I even tested this with so many enemies that when running the kill command I got 2 UPS. Everything still happened in the same tick and worms still 41 ticks later.

So I think I can safely assume the same will be the case with on_player_died and on_post_entity_died.

Thanks for the amazing game! :D
Post Reply

Return to “Modding help”