Page 1 of 1

Lua: Does an entity change it

Posted: Fri Oct 07, 2016 1:57 pm
by ssilk
My problem: I have a "general table" of all entities, that my mod should maintain. That list should also survive saves, so it is a global table.

From that table I want to make "fast" tables, that can be accessed by triggered events.

About like so:

Code: Select all

...
entityInfo = {
  entity = entity,
  state = calculateSomeState(entity),
...
}
...
table.insert(gobals.entitiestable[force.name], entityInfo)  -- adds one entityInfo to the global table
...
table.insert(my_fast_table[entity][entityInfo.state], entityInfo.entity) -- adds that entity to the "fast" table
...
The problem comes up, when I an entity is destroyed or changes. I need to remove it either fomr the globals.entitytable or from my_fast_table.
In both cases I want to lookup to the same entry in the other table.

So my idea about this was, that all entities are not added to a sorted list (1, 2, 3...) but to a hashed-table.
The problem is then: How do I get a useful hash?

My idea for that looks currently like so:

Code: Select all

...
entityInfo = {
  entity = entity,
  state = calculateSomeState(entity),
...
}

entitiyInfo.hash = tostring(entitiyInfo.entity) -- entitiyInfo.hash is now something like table: 0x123e47129

...
gobals.entitiestable[force.name][entityInfo.hash] = entityInfo  -- adds one entityInfo to the global table
...
my_fast_table[entity][entityInfo.state][entityInfo.hash] = entityInfo.entity -- adds that entity to the "fast" table
...
The question is: If now a new entity comes, that is in reality one, that is already existing in my globals table, will it have the same hash?

Re: Lua: Does an entity change it

Posted: Fri Oct 07, 2016 2:40 pm
by Adil
No, it doesn't. You're just converting an object reference into text, at different time a game might give you different reference to the same object.
Most entities have unit_number these days, which is unique identifier for them.
I also don't quite get why the second table is titled 'fast'.

Re: Lua: Does an entity change it

Posted: Fri Oct 07, 2016 3:02 pm
by ssilk
Thanks so far, Adil.
Adil wrote: I also don't quite get why the second table is titled 'fast'.
Because the post is - however - posted before it should have been posted. I'm sorry, the right post is viewtopic.php?f=25&t=33905