Page 1 of 1

Entity unit_number

Posted: Wed Oct 28, 2020 4:14 am
by PFQNiet
What types of entity have a unit_number? Is it just units? Or other things too?

On a similar topic, is it possible to get a LuaEntity by its unit number, or would I need to keep a table of [entity.unit_number] = entity?

Re: Entity unit_number

Posted: Wed Oct 28, 2020 7:16 am
by DaveMcW
Anything that can belong to a force also has a unit number.

No, you need to build your own table.

Re: Entity unit_number

Posted: Wed Oct 28, 2020 8:10 am
by darkfrei
You don't need the uid, but

You can make your own uid for entities such as

Code: Select all

local uid = generate_uid()
global.holders[uid] = {entity = entity, uid = uid}

Code: Select all

function generate_uid()
  local last_uid = global.last_uid or 0
  local new_uid = last_uid +1
  global.last_uid = new_uid
  return new_uid
end

Re: Entity unit_number

Posted: Wed Oct 28, 2020 9:59 am
by Klonan
darkfrei wrote: Wed Oct 28, 2020 8:10 am You don't need the uid, but

You can make your own uid for entities such as

Code: Select all

local uid = generate_uid()
global.holders[uid] = {entity = entity, uid = uid}

Code: Select all

function generate_uid()
  local last_uid = global.last_uid or 0
  local new_uid = last_uid +1
  global.last_uid = new_uid
  return new_uid
end
This doesn't help, for instance if you have a `entity_died` event, and you want to lookup the entity in your global list, how is this uid useful?

Unit number if useful because you can be given an entity from any event, and then easily and reliably lookup its data using the unit_number as the key.

Also you can get a better uid, reliable, by using https://lua-api.factorio.com/latest/Lua ... _destroyed, which works for all entities

Re: Entity unit_number

Posted: Thu Oct 29, 2020 1:22 am
by eradicator
Klonan wrote: Wed Oct 28, 2020 9:59 am Also you can get a better uid, reliable, by using https://lua-api.factorio.com/latest/Lua ... _destroyed, which works for all entities
Can you ellaborate how that is "better"? So far i assumed that it causes additional overhead and thus is a waste on anything that already has a unit_number.

Re: Entity unit_number

Posted: Thu Oct 29, 2020 9:35 am
by Klonan
eradicator wrote: Thu Oct 29, 2020 1:22 am
Klonan wrote: Wed Oct 28, 2020 9:59 am Also you can get a better uid, reliable, by using https://lua-api.factorio.com/latest/Lua ... _destroyed, which works for all entities
Can you ellaborate how that is "better"? So far i assumed that it causes additional overhead and thus is a waste on anything that already has a unit_number.
I mean, better than using Lua to assign a count