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.
[solved] entities returned by search always valid? = yes!
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
[solved] entities returned by search always valid? = yes!
Last edited by aubergine18 on Sat Oct 29, 2016 12:44 am, edited 1 time in total.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
-
- Filter Inserter
- Posts: 841
- Joined: Mon Sep 14, 2015 7:40 am
- Contact:
Re: are entities returned by search always valid?
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?
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?
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
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
Re: are entities returned by search always valid?
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.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Re: are entities returned by search always valid?
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.Choumiko wrote:As far as i know any entity you get directly via the API is valid. (excluding vanilla events raised by mods)
If you want to get ahold of me I'm almost always on Discord.