entity.id
Posted: Sun Sep 18, 2016 6:06 am
Hello,
It would be nice to have a unique identifier for each entity, for example entity.id, similar to player.index. My current work around is to concat strings together like so:
I had originally tried tostring(entity), however that is does not work in all cases, for example the value of tostring(player.selected) varies across ticks.
A couple disadvantages of this workaround:
- I don't know if it is 100% reliable, for example can two entities can have the same name, position, force name and surface name? If so this would fail.
- The returned id is longer than necessary, it would be nicer to have a uuid instead.
- String concatenation is slower: in this case it would be faster to do read 1 id property directly from memory.
- Calling entity_id(entity) is more verbose than entity.id.
It would be nice to have a unique identifier for each entity, for example entity.id, similar to player.index. My current work around is to concat strings together like so:
Code: Select all
-- Return a string representing the unique entity id
function entity_id(entity)
if not entity then return nil end
return entity.name .. "-" .. entity.position.x .. "-" .. entity.position.y .. "-" .. entity.surface.name .. "-" .. entity.force.name
end
A couple disadvantages of this workaround:
- I don't know if it is 100% reliable, for example can two entities can have the same name, position, force name and surface name? If so this would fail.
- The returned id is longer than necessary, it would be nicer to have a uuid instead.
- String concatenation is slower: in this case it would be faster to do read 1 id property directly from memory.
- Calling entity_id(entity) is more verbose than entity.id.