LuaGameScript:get_entity_by_unit_number()

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
folk
Long Handed Inserter
Long Handed Inserter
Posts: 96
Joined: Fri Mar 03, 2017 5:00 pm
Contact:

LuaGameScript:get_entity_by_unit_number()

Post 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)?

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaGameScript:get_entity_by_unit_number()

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

Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: LuaGameScript:get_entity_by_unit_number()

Post 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?).

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: LuaGameScript:get_entity_by_unit_number()

Post 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.

folk
Long Handed Inserter
Long Handed Inserter
Posts: 96
Joined: Fri Mar 03, 2017 5:00 pm
Contact:

Re: LuaGameScript:get_entity_by_unit_number()

Post 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.

Post Reply

Return to “Modding interface requests”