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

Place to get help with not working mods / modding interface.
Post Reply
Schallfalke
Fast Inserter
Fast Inserter
Posts: 162
Joined: Sun Oct 28, 2018 7:57 am
Contact:

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

Post 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
Last edited by Schallfalke on Tue Feb 16, 2021 10:20 pm, edited 1 time in total.

Bilka
Factorio Staff
Factorio Staff
Posts: 2849
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: LuaEntity::status to Locale text relations?

Post 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]
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Schallfalke
Fast Inserter
Fast Inserter
Posts: 162
Joined: Sun Oct 28, 2018 7:57 am
Contact:

Re: LuaEntity::status to Locale text relations?

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

Post Reply

Return to “Modding help”