entity.id

Things that already exist in the current mod API
lux
Inserter
Inserter
Posts: 25
Joined: Fri Nov 07, 2014 7:40 pm
Contact:

entity.id

Post 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.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: entity.id

Post by Nexela »

try entity.unit_number

However I believe some entities (like doodads) don't have unit_num
Rseding91
Factorio Staff
Factorio Staff
Posts: 14360
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: entity.id

Post 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.
If you want to get ahold of me I'm almost always on Discord.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: entity.id

Post 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).
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14360
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: entity.id

Post 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.
If you want to get ahold of me I'm almost always on Discord.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: entity.id

Post 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?
Rseding91
Factorio Staff
Factorio Staff
Posts: 14360
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: entity.id

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

Return to “Already exists”