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

Place to post guides, observations, things related to modding that are not mods themselves.
xyzzycgn
Inserter
Inserter
Posts: 20
Joined: Tue Dec 24, 2024 7:37 pm
Contact:

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

Post 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?
Rseding91
Factorio Staff
Factorio Staff
Posts: 16271
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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
If you want to get ahold of me I'm almost always on Discord.
xyzzycgn
Inserter
Inserter
Posts: 20
Joined: Tue Dec 24, 2024 7:37 pm
Contact:

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

Post 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
Rseding91
Factorio Staff
Factorio Staff
Posts: 16271
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post by Rseding91 »

There are many ways for entities to go away without an explicit event. The above link handles them all guaranteed.
If you want to get ahold of me I'm almost always on Discord.
robot256
Smart Inserter
Smart Inserter
Posts: 1309
Joined: Sun Mar 17, 2019 1:52 am
Contact:

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

Post 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.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
xyzzycgn
Inserter
Inserter
Posts: 20
Joined: Tue Dec 24, 2024 7:37 pm
Contact:

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

Post 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 👍
Bilka
Factorio Staff
Factorio Staff
Posts: 3672
Joined: Sat Aug 13, 2016 9:20 am
Contact:

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

Post by Bilka »

You can use on_surface_deleted and on_surface_cleared for surface deletions or calls to LuaSurface.clear.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
xyzzycgn
Inserter
Inserter
Posts: 20
Joined: Tue Dec 24, 2024 7:37 pm
Contact:

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

Post 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 ;)
Rseding91
Factorio Staff
Factorio Staff
Posts: 16271
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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
If you want to get ahold of me I'm almost always on Discord.
xyzzycgn
Inserter
Inserter
Posts: 20
Joined: Tue Dec 24, 2024 7:37 pm
Contact:

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

Post 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:
Post Reply

Return to “Modding discussion”