Page 1 of 1

[Solved] LuaEntity::status to Locale text relations?

Posted: Sun Jan 31, 2021 6:52 pm
by Schallfalke
Hi all,

I want to write a custom GUI showing entity status of some selected entities.
LuaEntity::status gives enum like "defines.entity_status.item_ingredient_shortage", which will actually be integers.
On the other hand, the text I want to show is located in locale files under [entity-status], like the line "item-ingredient-shortage=Item ingredient shortage".

Here are my questions:
1/ How can I use the value read from LuaEntity::status, to get the corresponding status locale strings?
2/ Is there a function (which I may have overlooked) to achieve Q1?
3/ If No & No to Q1 & Q2. Is there a smarter way than construct my own table for such integer-to-string mapping... I am currently achieving my Q1 by writing a lengthy table (shown below) by using defines.entity_status reference.

Code: Select all

local entity_status = {
  [defines.entity_status.working] =                           { code = 1, desc = "working" },
  [defines.entity_status.normal] =                            { code = 1, desc = "normal" },
  [defines.entity_status.no_power] =                          { code = 3, desc = "no-power" },
  [defines.entity_status.low_power] =                         { code = 2, desc = "low-power" },
  [defines.entity_status.no_fuel] =                           { code = 3, desc = "no-fuel" },
  ...
}

local status = player.selected.status
local msg = { "entity-status." .. entity_status[status].desc }
local coloridx = entity_status[status]
4/ Follow-up of Q3. How did devs work out the entity status used in entity GUIs in the 1.1.0 update? I am sure devs should have done it better than my lengthy table above. And I know devs do not write in Lua, but I want to know what materials I may have access to...

Thanks for your attention.

Regards,
Schall

Re: LuaEntity::status to Locale text relations?

Posted: Tue Feb 16, 2021 11:23 am
by Bilka

Code: Select all

local lookup = {}
for stat, number in pairs(defines.entity_status) do
  lookup[number] = {"entity-status." .. stat:gsub("_", "-")}
end
local status = player.selected.status
local msg = lookup[status]

Re: LuaEntity::status to Locale text relations?

Posted: Tue Feb 16, 2021 10:19 pm
by Schallfalke
Wow, what a smart code! Thanks for that.
It waives the need of my explicit table, and auto-handles all possible changes. Exactly what I needed!

Though will need separate code to assign "status colours" to each status.
Looks easy enough, so no big deal.