FactorioLua: Does an entity change it's adress over livetime?
Posted: Fri Oct 07, 2016 2:30 pm
My problem: I have lists of entities that are maintained in my code.
Now when an event comes in, I need to look up in general (not always), if that entity is already maintained or not.
About like so:
Well. That list could be quite long (some hundred entries) and doing always such a search-for is simply spoken stupid, cause LUA supports hashes.
What I really want is something like
My idea for such a hash method is so:
Using tostring() returns the memory-adress of that object as string. Cause two objects cannot exist at the same address this should work. I mean in pure Lua I have tested this and it works.
The question: Is such an entity to be guaranteed on the same memory address over the instance of a running game? And if the address changes, is that already handled by events?
I mean: Logically it is not, when a game is saved and loaded, but during a game a game-object should not change it's adress uselessly? So might be possible, or?
PS: The reason why I ask this is also, because when I manage several lists in my mod, that holds the same entity (one general list with all the entities, some optimized lists for different purposes), I need some kind of "Primary Key" for the entities, so that delete-operations can be really quick. Perhaps there is also a different/simpler approach?
Now when an event comes in, I need to look up in general (not always), if that entity is already maintained or not.
About like so:
Code: Select all
...
function handle_my_event(event)
...
index = find_index(event.entity)
DO STUFF
...
function find_index(event_entity)
for i, listed_entity in pairs(globals.mylist[event_entity.force.name])
if listed_entity.valid and listed_entity == event_entity then
return i -- the found index of that entry
end
end
What I really want is something like
Code: Select all
...
function handle_my_event(event)
local hash = SOME_HASH_FUNCTION_THAT_RETURNS_UNIQUE_HASH_FOR_ENTITY(event.entity)
local listed_entity = globals.mylist[event.entity.force.name][hash]
if listed_entity.valid then
DO STUFF
end
Code: Select all
...
function handle_my_event(event)
local hash = tostring(event.entity) -- generates something like "table: 0xAE7840048"
local listed_entity = globals.mylist[event.entity.force.name][hash]
...
The question: Is such an entity to be guaranteed on the same memory address over the instance of a running game? And if the address changes, is that already handled by events?
I mean: Logically it is not, when a game is saved and loaded, but during a game a game-object should not change it's adress uselessly? So might be possible, or?
PS: The reason why I ask this is also, because when I manage several lists in my mod, that holds the same entity (one general list with all the entities, some optimized lists for different purposes), I need some kind of "Primary Key" for the entities, so that delete-operations can be really quick. Perhaps there is also a different/simpler approach?