How to get recipe icons of all recipes [SOLVED]

Place to get help with not working mods / modding interface.
M.Colcko
Inserter
Inserter
Posts: 21
Joined: Sun Dec 04, 2016 7:23 am
Contact:

How to get recipe icons of all recipes [SOLVED]

Post by M.Colcko »

I'm trying to create a sprite button table filled with every recipe in the game (including mods). The best way to do that seems to be looping through the recipe prototypes in game.players[playerIndex].force.recipes. However, I am uncertain how to get the recipe icons for each recipe. Just using "item/recipe.name" doesn't work for, for example, fluid recipes, creating dozens of ifs isn't really feasible, and I don't even know if "item/recipe.name" would work for modded stuff.
Using Lua API global Variable Viewer, the recipes in force don't contain "icon" even though the recipe Prototype definition does? Then how is it possible to retrieve the icon of a recipe, and making it mod compatible. Looping through force.recipes really seems to be the way, since that contains all recipes in the whole game, modded included.

Code snippet of my loop (currently set up to only print the icon of the recipe):

Code: Select all

function build_sprite_buttons(player)
    local button_table = player.gui.screen["cc_configWindow"].cc_contentFrame.button_frame.button_table
    button_table.clear()

    for _, recipe in pairs(player.force.recipes) do
        game.print(tostring(recipe.icon))
    end
end
Returns this error:
Error while running event crafting_cleanup::on_player_created (ID 26)
LuaRecipe doesn't contain key icon.
stack traceback:
[C]: in function '__index'
__crafting_cleanup__/control.lua:134: in function 'build_sprite_buttons'
__crafting_cleanup__/control.lua:246: in function 'createConfigWindow'
__crafting_cleanup__/control.lua:259: in function <__crafting_cleanup__/control.lua:250>
Last edited by M.Colcko on Thu Aug 04, 2022 8:35 am, edited 1 time in total.
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: How to get recipe icons of all recipes

Post by Bilka »

Sprite button sprites are specified with a SpritePath, which has a type for recipes. So, just use "recipe/iron-gear-wheel" and so on.

Console command example:

Code: Select all

/c game.player.gui.top.add{type="sprite-button", sprite="recipe/iron-gear-wheel"}
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
M.Colcko
Inserter
Inserter
Posts: 21
Joined: Sun Dec 04, 2016 7:23 am
Contact:

Re: How to get recipe icons of all recipes

Post by M.Colcko »

Just tried it, and it works just as I hoped it would. Thank you!
Post Reply

Return to “Modding help”