Page 1 of 1

LuaGameScript:get_entity_by_unit_number()

Posted: Tue May 02, 2017 10:47 pm
by folk
Is there any particular reason why we are unable to get a LuaEntity from a unit_number (which would obviously fail silently for not-existing ones)?

Re: LuaGameScript:get_entity_by_unit_number()

Posted: Wed May 03, 2017 1:32 am
by Rseding91
It's an O(N) search - equivalent to you doing "for k,v in pairs(game.player.surface.find_entities_filtered{}) do if v.unit_number == ## then .. end end"

Re: LuaGameScript:get_entity_by_unit_number()

Posted: Wed May 03, 2017 1:52 am
by Supercheese
It would be much better to simply store entities of interest in a custom global lookup table using unit_number as an index, no? Just add 'em each time they're deemed to be of relevance after some event and remove 'em once they've been processed satisfactorily (satisFactorio-ly?).

Re: LuaGameScript:get_entity_by_unit_number()

Posted: Wed May 03, 2017 1:57 am
by Nexela
Rseding91 wrote:It's an O(N) search - equivalent to you doing "for k,v in pairs(game.player.surface.find_entities_filtered{}) do if v.unit_number == ## then .. end end"
That would fail real quick with attempt to compare number to nil :)

On a more serious note, What about having unit_number be an additional search parameter in find_filtered?
unit_number = true that way the returned search would only contain entities with unit_numbers.

Re: LuaGameScript:get_entity_by_unit_number()

Posted: Wed May 03, 2017 8:01 am
by folk
Rseding91 wrote:It's an O(N) search - equivalent to you doing "for k,v in pairs(game.player.surface.find_entities_filtered{}) do if v.unit_number == ## then .. end end"
Yes, but it's O(n) without passing essentially entire game state through the lua bridge, not to mention the creation of luaentity representations for things that previously were untouched by the lua bridge?

I seem to remember someone saying somewhere that the relevant lua representations of game objects were lazily created at least.

But if you're telling me that it would essentially be as fast and that there's very little difference - and I'm happy with both "essentially as fast" and "very little" being unquantified - that's good enough for me.