Page 1 of 1

Retrieving Entity Icons. How?

Posted: Tue Jan 28, 2025 5:39 pm
by Sheridan
Hello everyone,

I'm currently developing a mod in Factorio and I've run into an issue I haven't been able to solve for a couple of days now. I have a list of entity prototypes in an array in control.lua obtained from a selection or a blueprint object list. My goal is to count the number of entities by type and create a table where one of the cells in each row contains a button with the entity's icon (be it an entity or a tile).

Here's a snippet of my code:

Code: Select all

for type, count in pairs(entities) do
    local prototype = defines.prototypes.entity[type]
    if prototype ~= nil then
        -- ...
    end
end
I haven't found the icons in defines.prototypes. It seems that prototype classes don't have icons either. I could hard-code the paths to the icons, but that would only work with the vanilla game, and I wouldn't be able to get the modded entities' icons this way.

Does anyone know where I can obtain the entity's icon? Any help would be greatly appreciated!

Thank you!

Re: Retrieving Entity Icons. How?

Posted: Tue Jan 28, 2025 9:20 pm
by Sheridan
QwenLM helped me.
First: not `defines`, just `prototypes`
Second: not icons but sprites

Code: Select all

  for type, count in pairs(count_selection_entities(entities))
  do
    local prototype = prototypes['entity'][type]
    if prototype ~= nil
    then
      local sprite =  "entity/" .. type
    end
  end

Re: Retrieving Entity Icons. How?

Posted: Wed Jan 29, 2025 11:00 am
by Xorimuth
Sheridan wrote: Tue Jan 28, 2025 9:20 pm QwenLM helped me.
First: not `defines`, just `prototypes`
Second: not icons but sprites

Code: Select all

  for type, count in pairs(count_selection_entities(entities))
  do
    local prototype = prototypes['entity'][type]
    if prototype ~= nil
    then
      local sprite =  "entity/" .. type
    end
  end
Looks like you mostly got it (but your solution isn't quite correct). For future reference, here's how you'd work it out using the actual documentation:

You're creating a GUI, so refer to https://lua-api.factorio.com/latest/cla ... ement.html and see that it says "For information on all supported GUI elements, see GuiElementType." You're making buttons of icons so you want `sprite-button`. (Not that 'sprite' just means image. In factorio, all graphics are sprites, both ingame graphics and icons. `sprite-button` is typically used with icons.)

You can define which sprite is shown on a sprite-button using https://lua-api.factorio.com/latest/cla ... tml#sprite (or use the `sprite` parameter of LuaGuiElement::add directly). It says that excepts something of type SpritePath which has quite a lot of options, make sure to read the page carefully. In your case, as you want to display icons of entities, you can see that it says
"entity" - for example "entity/small-biter" is the icon sprite of the small biter
so all you need is the `name` of your entity.

Since you already have the name of your entity, you don't actually need to use `prototypes['entity']` at all, just do `"entity/" .. my_entity_name`.

There's also potentially a bit of confusion from your choice of variable names. You've called that variable `type` but it is probably should be called `name`. Things in factorio have both a `name` and a `type`, and sometimes they are the same, but often they are different. E.g. all transport belts have type `transport-belt` but their names are `transport-belt`/`fast-transport-belt`/`express-transport-belt`.

So, in summary, I'd change your code to this:

Code: Select all

  for name, count in pairs(count_selection_entities(entities))
  do
      local sprite =  "entity/" .. name
      if helpers.is_valid_sprite_path(sprite) then
        -- Do stuff in here. I'm not sure if the above check is actually necessary, it depends on what count_selection_entities is returning.
      end
  end
  

Re: Retrieving Entity Icons. How?

Posted: Wed Jan 29, 2025 3:11 pm
by Sheridan
Thank you for the help!

I have mostly figured things out. Understanding that in the runtime, images are not icons but sprites broke the barrier of misunderstanding for me, and everything fell into place.

As for prototypes, I'm using them to obtain localized names and descriptions of entities, but I didn't mention it because that part was clear to me.

Currently, my code looks like this:

Code: Select all

local function open_gui(player, counted)
  if player.opened then player.opened = nil end
  local gui = add_window(player, "counter-gui", {"counter_gui.window_caption"})
  local count_table = gui.add{
    type = "table",
    name = "count_table",
    style = "counter_table_style",
    column_count = 3,
    vertical_centering = true
  }
  for key, mined_prototypes in pairs(counted)
  do
    local prototype = prototypes.entity[mined_prototypes.prototype_name]
    if prototype ~= nil
    then
      local quality = prototypes.quality[mined_prototypes.quality_name]
      -- if prototype.is_building

      local button = count_table.add{
        type         = "sprite-button",
        name         = key .. "_info_button",
        style        = "counter_button_style",
        tooltip      = {"?", {"", prototype.localised_description, "\n"}, prototype.localised_name, ""},
        sprite       = "entity/" .. prototype.name,
        tags         = { prototype = prototype },
        elem_tooltip = {
                          type    = 'item-with-quality',
                          name    = prototype.name,
                          quality = quality.name,
                       }
      }
      if quality.name ~= "normal"
      then
        button.add{
          type             = 'sprite',
          style            = 'counter_quality_style',
          sprite           = 'quality/' .. quality.name,
          resize_to_sprite = false
        }
      end
      count_table.add{ type = "label", caption = prototype.localised_name }
      count_table.add{ type = "label", caption = mined_prototypes.count }
    end
  end
end
Right now, I've decided to create a "class" for the GUI table to include sorting, headers, and maybe some other useful features.