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!
[1.1.101] Map editor right-click entity deconstruction doesn't trigger events
- blargh2015
- Inserter
- Posts: 37
- Joined: Sun Jun 03, 2018 3:47 pm
- Contact:
-
- Smart Inserter
- Posts: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
From that thread you linked:
Bilka wrote: Mon Feb 05, 2024 10:58 amJust 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.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.
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.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics
- blargh2015
- Inserter
- Posts: 37
- Joined: Sun Jun 03, 2018 3:47 pm
- Contact:
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
Yes... and from the API documentation on the on_entity_destroyed event:
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.
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.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.
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.
-
- Smart Inserter
- Posts: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
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.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics
- Stringweasel
- Filter Inserter
- Posts: 455
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
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.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).
Alt-F4 Author | Factorio Modder
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
- Stringweasel
- Filter Inserter
- Posts: 455
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
For reference, here is the person - who wrote the profiler you used - saying that .valid is the cheapest possible API call.
Alt-F4 Author | Factorio Modder
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
-
- Smart Inserter
- Posts: 2767
- Joined: Tue Apr 25, 2017 2:01 pm
- Contact:
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
As that is in Discord, you may want to screenshot and paste it here for those that don't use Discord.Stringweasel wrote: Wed Feb 07, 2024 8:50 am For reference, here is the person - who wrote the profiler you used - saying that .valid is the cheapest possible API call.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics
Re: [1.1.101] Map editor right-click entity deconstruction doesn't trigger events
If you want to discuss this further, open a modding help topic or make an API request.