Page 1 of 1
[solved] entities returned by search always valid? = yes!
Posted: Sat Oct 22, 2016 8:05 pm
by aubergine18
Are entities returned by search functions such as `LuaSurface.find_entities_filtered()` always valid?
I have a situation that could potentially return a large number of entities from that search - can I safely omit the .valid check in this specific instance? I'm not caching the results, I'm iterating them immediately after doing the search.
Re: are entities returned by search always valid?
Posted: Sat Oct 22, 2016 8:52 pm
by Supercheese
aubergine18 wrote:can I safely omit the .valid check in this specific instance?
![Image](https://forums.factorio.com/images/ext/a78044f1ab040757e5d58d306bc87afc.gif)
Re: are entities returned by search always valid?
Posted: Sat Oct 22, 2016 9:25 pm
by Choumiko
As far as i know any entity you get directly via the API is valid. (excluding vanilla events raised by mods)
Re: are entities returned by search always valid?
Posted: Sat Oct 22, 2016 9:35 pm
by Klonan
aubergine18 wrote:Are entities returned by search functions such as `LuaSurface.find_entities_filtered()` always valid?
I have a situation that could potentially return a large number of entities from that search - can I safely omit the .valid check in this specific instance? I'm not caching the results, I'm iterating them immediately after doing the search.
They are valid when you initially find them, but not if you do something to them after...
Code: Select all
local entities = surface.find_entities(area)
for k = 1, #entities do
local this = entities[k]
local that = entities[k+1]
if this.valid then
that.destroy()
game.print(k)
end
end
All the entities were valid when the find_entities was called. But they can be invalidated by whatever you do with your script. It doesn't really hurt to do a entity.valid check, but if you're certain you're not going to be invalidating it, then it should be okay.
Re: are entities returned by search always valid?
Posted: Sat Oct 22, 2016 9:44 pm
by aubergine18
In the loop I'm working out which of the entities in the list are closest to another entity (which is already checked as valid), so no changes being made in that iteration.
Re: are entities returned by search always valid?
Posted: Sat Oct 22, 2016 11:03 pm
by Rseding91
Choumiko wrote:As far as i know any entity you get directly via the API is valid. (excluding vanilla events raised by mods)
Entities are valid even when an event is raised from a mod. The event is not called if the object-to-be-passed is invalid at any point.