Page 1 of 1

entity.id

Posted: Sun Sep 18, 2016 6:06 am
by lux
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:

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

Re: entity.id

Posted: Sun Sep 18, 2016 6:14 am
by Nexela
try entity.unit_number

However I believe some entities (like doodads) don't have unit_num

Re: entity.id

Posted: Sun Sep 18, 2016 9:03 am
by Rseding91
It's an issue of RAM and save file size. If every entity had a unique ID it would almost be required to be a 64 bit number (8 bytes).

Now take your average "big" save which has between 5 million and 15 million entities and say 10 million is how many a "big" save has.

10,000,000 * 8 bytes = 80,000,000 bytes = 80,000,000 / 1024 / 1024 = 76.29~ MB of RAM just to store the IDs

Now assume those compress well in the save file and you only end up saving 4 bytes of data on average after compression. That's still 38.145~ MB of data in the save file just to save IDs.

Re: entity.id

Posted: Sun Sep 18, 2016 9:07 am
by aubergine18
doodads not having entity id isn't too much of a problem IMO

we only really need entity ids for things that the player can interact with (stuff that's buildable).

Re: entity.id

Posted: Sun Sep 18, 2016 9:16 am
by Rseding91
aubergine18 wrote:doodads not having entity id isn't too much of a problem IMO

we only really need entity ids for things that the player can interact with (stuff that's buildable).
That's what unit_number is now - anything with an owner (anything with a force) has a unit number.

Re: entity.id

Posted: Sun Sep 18, 2016 9:23 am
by Nexela
Rseding91 wrote:That's what unit_number is now - anything with an owner (anything with a force) has a unit number.
That is what my thoughts were, any chance you can notate this in the api?

Re: entity.id

Posted: Sun Sep 18, 2016 9:28 am
by Rseding91
Nexela wrote:
Rseding91 wrote:That's what unit_number is now - anything with an owner (anything with a force) has a unit number.
That is what my thoughts were, any chance you can notate this in the api?
Well, the term "anything with an owner" doesn't help much when it comes to the API. A player can "own" some trees but that doesn't mean the C++ class for trees is "EntityWithOwner".