Page 1 of 1
[1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Mon Feb 05, 2024 10:21 pm
by blargh2015
Note: This is not a repeat of
viewtopic.php?p=452117#p452117 - that deals with the deconstruction planner. This is the same type of issue, but in a different mechanism.
I have a mod that numerically tracks the utilization of most entities that produce things (UtilizationMonitorBlargh). As such, it is set to take events on new entities created and destroyed, so it can start tracking on create, and cleanup its data on destroy.
In the map /editor, if you go to None mode, and place down items (assemblers, etc) - the mod gets the entity created signal just fine and does its thing. However, if you right click delete the item, no matching signal on destruction is raised. This means when it's destroyed, I have to rely on an extra entity.valid == true check in my on_tick code to catch this. (Yes, I'm aware occasion mods fail to signal as well but those tend to get fixed quickly, and the entity.valid == true check is relatively expensive compared to the rest of the code - that one check is about 10% of its runtime according to the profiler).
In the thread above, this issue was fixed for the deconstruction planner, but apparently not fixed for manual entity removal. Any chance we can get the same fix there?
Thanks!
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Mon Feb 05, 2024 11:33 pm
by FuryoftheStars
From that thread you linked:
Bilka wrote: Mon Feb 05, 2024 10:58 am
DaveMcW wrote:
2. Enter map editor
3. Make sure Settings -> Instant deconstruction is on.
4. Select Tools -> None.
5. Pick up a deconstruction planner and deconstruct something.
Just to make this clear for anyone coming across this thread: The script_raised_destroy event was only added for the case of instant deconstruction via using a deconstruction planner in the map editor, quoted above.
Destroying an entity by using the "entity" tab in the map editor and "mining" it does NOT raise an event by default (as of writing this post). If you want to be notified of an entity being destroyed via the map editor entity tab, register it for the
on_entity_destroyed and listen to the event. This event is raised for any kind of entity destruction (once registered for the entity), including but not limited to using the entity tab in the map editor, chunks or surfaces being removed, or just plain old things like a player mining the entity.
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Wed Feb 07, 2024 2:09 am
by blargh2015
Yes... and from the API documentation on the on_entity_destroyed event:
Depending on when a given entity is destroyed, this event will be fired at the end of the current tick or at the end of the next tick.
Unfortunately, this makes it useless for my use, as I can still have cases where I haven't gotten the destroy signal in time to remove it from my data before the on_tick runs next, and references an invalid LuaEntity because it's already destroyed, just the signal hasn't gotten to the mod yet. Thus, I still need the expensive valid check.
Just to double check this, I implemented using only the on_entity_destroyed event in the mod (with registering it in the handler for entity creation) and immediately errored out when using other mods that trigger destruction.
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Wed Feb 07, 2024 7:44 am
by FuryoftheStars
Unfortunately, due to several factors, not the least of which being that many modders will call .destroy() without setting the raise event true, you should always do the valid check, regardless.
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Wed Feb 07, 2024 8:42 am
by Stringweasel
blargh2015 wrote: Mon Feb 05, 2024 10:21 pm
(Yes, I'm aware occasion mods fail to signal as well but those tend to get fixed quickly, and the entity.valid == true check is relatively expensive compared to the rest of the code - that one check is about 10% of its runtime according to the profiler).
Not sure how you tested the performance of this call, but the `entity.valid` check should be super cheap performance-wise. Can't remember how it works internally, but I do remember someone with a lot of knowledge saying it's likely one of the cheapest API calls there is.
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Wed Feb 07, 2024 8:50 am
by Stringweasel
For reference, here is the person - who wrote the profiler you used - saying that .valid is the
cheapest possible API call.
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Wed Feb 07, 2024 1:25 pm
by FuryoftheStars
As that is in Discord, you may want to screenshot and paste it here for those that don't use Discord.
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Posted: Wed Feb 07, 2024 1:33 pm
by Loewchen
If you want to discuss this further, open a modding help topic or make an API request.