Please add entity_unit_number to on_post_entity_died event

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

Please add entity_unit_number to on_post_entity_died event

Post by mrvn »

TL;DR
The on_post_entity_died event should include a way to associate the event with the entity that died.
What ?
The event for on_post_entity_died should include a field entity_unit_number containing the unit_number of the entity that died or nil.
Why ?
In many mods the entities unit_number is used as key to store information about entities. The information is usually created when an entity is built and removed when an entity is mined or destroyed. Unlike entities or other keys the unit_number is preserved across game save and load so the mods global data remains valid and remains associated to the same entity.

Now in 0.17.67 LuaItemStack.set_blueprint_entity_tag() and LuaEntity.tags for ghosts was added so mods could store custom data in blueprints and ghost to recreate an entities configuration when the the entity is blueprinted. I want to use the same so that the configuration of an entity is preserved when it dies and is rebuild from the ghost.

In on_entity_died() the event contains the entity that has died before it gets destroyed. But at that time the ghost does not yet exist. In on_post_entity_died() the event contains the ghost but no reference is made to the (now invalid) entity and the entity no longer exists. So mods have to save the entities data in on_entity_died() to some temporary location and transfer it to tags in on_post_entity_died(). This is complicated by the fact that multiple on_entity_died() events can be triggered recursively (the mod can kill another entity in on_entity_died) and then on_post_entity_died() events happen in the reverse order. So it's not enough to simply remember the last entity that died.

This makes it hard to copy configuration from the entity into tags of the ghost. But if the event also included the originals entity.unit_number then that could be used to lookup the entities LUA data in the mod, transfer that to the ghosts tags and remove the data from the mods lookup table. In most cases a on_entity_died() event handler wouldn't be needed anymore and complex mods would have a simple way of associating the ghosts with previous entities.

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

Re: Please add entity_unit_number to on_post_entity_died event

Post by Bilka »

Moved to modding interface requests
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
LuziferSenpai
Filter Inserter
Filter Inserter
Posts: 333
Joined: Tue Jul 08, 2014 10:06 am
Contact:

Re: Please add entity_unit_number to on_post_entity_died event

Post by LuziferSenpai »

That can be solved with having something like this:

Code: Select all

if entity.valid then
else
    remove the entry
end
Coding is awesome!
Animes are love!
Factorio is life!

My MODs:
Click

Greetz,

Senpai

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Please add entity_unit_number to on_post_entity_died event

Post by eradicator »

mrvn wrote:
Sat Sep 14, 2019 3:44 pm
In on_post_entity_died() the event contains the ghost but no reference is made to the (now invalid) entity and the entity no longer exists.
LuaEntity.ghost_unit_number
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Please add entity_unit_number to on_post_entity_died event

Post by eradicator »

eradicator wrote:
Sun Sep 15, 2019 9:30 pm
mrvn wrote:
Sat Sep 14, 2019 3:44 pm
In on_post_entity_died() the event contains the ghost but no reference is made to the (now invalid) entity and the entity no longer exists.
LuaEntity.ghost_unit_number
Which is sadly insufficent if an entity dies without leaving a ghost. Thus:

+1 for including the unit_number in the event.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: Please add entity_unit_number to on_post_entity_died event

Post by Bilka »

Okay, added in the next version. Please keep in mind that the unit number is optional (not all entities have one).
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: Please add entity_unit_number to on_post_entity_died event

Post by mrvn »

Bilka wrote:
Mon Sep 23, 2019 1:02 pm
Okay, added in the next version. Please keep in mind that the unit number is optional (not all entities have one).
Thanks. For the purpose of removing entity data indexed by unit_number in modes the later is a non-issue. Any entity indexed by unit_number obviously has one. Not sure how you would manage other entities in LUA.

Post Reply

Return to “Implemented mod requests”