Page 1 of 1

No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 9:03 am
by xyzzycgn
Hi altogether,

when I use "remove all entities" in editor mode, it seems so that there is no event triggered for that.

Although it's obvious that there can't / shouldn't be an event for each entity removed from a surface, i would expect that at least one event is triggered for this action to have a chance to react to it - IMO on_surface_cleared (which is raised when importing a save file) would be fine.

Is the absence of any event really intended behaviour?

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 11:10 am
by Rseding91
xyzzycgn wrote: Mon Oct 20, 2025 9:03 amIs the absence of any event really intended behaviour?
Yes. You can use https://lua-api.factorio.com/latest/cla ... _destroyed

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 11:39 am
by xyzzycgn
Rseding91 wrote: Mon Oct 20, 2025 11:10 am Yes. You can use https://lua-api.factorio.com/latest/cla ... _destroyed
Hmm - sounds a bit weird - and quite cumbersome ;) - for me to register a lot of distinct entities and to raise a corresponding number of events instead of a single one, but it is what it is.

Thx for your quick answer

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 11:41 am
by Rseding91
There are many ways for entities to go away without an explicit event. The above link handles them all guaranteed.

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 12:07 pm
by robot256
This is also the reason that checking any stored Lua object references for .valid==true before use is important even if you don't think it should have changed. Editor causes multiple things to happen that don't in normal gameplay.

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 12:20 pm
by xyzzycgn
Ah - didn't see at first that it is possible to register the surface itself with https://lua-api.factorio.com/latest/cla ... _destroyed - and that is what I need 👍

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 12:41 pm
by Bilka
You can use on_surface_deleted and on_surface_cleared for surface deletions or calls to LuaSurface.clear.

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 1:22 pm
by xyzzycgn
Sorry - my fault. Didn't double check the usability of registering the surface itself with https://lua-api.factorio.com/latest/cla ... _destroyed for my problem. I had overlooked that using "remove all entities" in editor mode doesn't delete the surface at all :oops: .
Bilka wrote: Mon Oct 20, 2025 12:41 pm You can use on_surface_deleted and on_surface_cleared for surface deletions or calls to LuaSurface.clear.
And the lack of any event being risen by the usage of "remove all entities" is the core of my problem - if on_surface_cleared would be risen when using it, everything would be fine and convenient.

So registering all relevant entities on the surface with https://lua-api.factorio.com/latest/cla ... _destroyed is a valid solution - although not my favorite ;)

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 2:08 pm
by Rseding91
You'll get the same behavior if any mod simply calls destroy() on anything, or if someone enters a simple command such as:

Code: Select all

for _,v in pairs(game.player.surface.find_entities_filtered{}) do
  v.destroy()
end

Re: No event is raised when using "remove all entities" in editor mode

Posted: Mon Oct 20, 2025 3:00 pm
by xyzzycgn
Rseding91 wrote: Mon Oct 20, 2025 2:08 pm You'll get the same behavior if any mod simply calls destroy() on anything, or if someone enters a simple command such as:

Code: Select all

for _,v in pairs(game.player.surface.find_entities_filtered{}) do
  v.destroy()
end
A good hint - I hadn't noticed that yet. Thx for the clarification 👍

P.S.: Such nasty mods should be forbidden :lol: