Looking for event (destroyed entities)

Place to get help with not working mods / modding interface.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1759
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Looking for event (destroyed entities)

Post by Pi-C »

I've received a bug report yesterday because of a crash that involved accessing an invalid entity. The bug report was a case of landfill accidentally placed over an entity on water, but the same thing also happened when waterfill is placed over an entity on land.

I want to react to entities disappearing, but I haven't found a proper event yet. They are not mined, so neither on_X_pre_mined nor on_X_mined_entity trigger. The entities also don't die, so they also don't trigger on_entity_died or script_raised_destroy. Is there an easy way to check for entities that have simply disappeared?

I suppose the entities vanish because their collision mask isn't compatible with that of the placed tile. So I probably could react to on_X_built_tile. But this only triggers after a tile has been placed, and it doesn't return the entities that were at that position. I will get the position, so I could loop over the list of stored entities to get their position (except the entities don't exist anymore, so I'd get a "trying to index nil" error). Listening to on_put_item, checking if the item has a placed_as_tile result, checking the collision mask of the tile, looking for entities in that position, and comparing their collision mask with that of the tile could work -- but it's a lot of work, and landfill is usually placed en masse, so that wouldn't be UPS friendly. Does anybody have a better idea?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5411
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Looking for event (destroyed entities)

Post by Klonan »

Pi-C wrote: Thu May 14, 2020 12:38 pmDoes anybody have a better idea?
Just check that its valid before trying to do things with it...

There are many many ways entities can become invalidated without any event notification.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1759
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Looking for event (destroyed entities)

Post by Pi-C »

Klonan wrote: Thu May 14, 2020 12:43 pm
Pi-C wrote: Thu May 14, 2020 12:38 pmDoes anybody have a better idea?
Just check that its valid before trying to do things with it...
Thanks! I was hoping I could avoid that. There are always those cases where you forget to check if an entity is valid -- and that will crash the game. Being notified when a relevant entity becomes invalid would allow me to prune my lists, but …
There are many many ways entities can become invalidated without any event notification.
… I guess that would also put a lot of load onto the system, so there's no chance to get such an event implemented. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Post Reply

Return to “Modding help”