[solved] entities returned by search always valid? = yes!

Place to get help with not working mods / modding interface.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

[solved] entities returned by search always valid? = yes!

Post 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.
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.
Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: are entities returned by search always valid?

Post by Supercheese »

aubergine18 wrote:can I safely omit the .valid check in this specific instance?
Image
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: are entities returned by search always valid?

Post by Choumiko »

As far as i know any entity you get directly via the API is valid. (excluding vanilla events raised by mods)
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5341
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: are entities returned by search always valid?

Post 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.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: are entities returned by search always valid?

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

Re: are entities returned by search always valid?

Post 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.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding help”