Entity unit_number

Place to get help with not working mods / modding interface.
Post Reply
PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Entity unit_number

Post 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?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Entity unit_number

Post by DaveMcW »

Anything that can belong to a force also has a unit number.

No, you need to build your own table.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Entity unit_number

Post 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

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Entity unit_number

Post 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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Entity unit_number

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Entity unit_number

Post 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

Post Reply

Return to “Modding help”