Page 1 of 1

How to get recipe icons of all recipes [SOLVED]

Posted: Wed Aug 03, 2022 7:18 pm
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>

Re: How to get recipe icons of all recipes

Posted: Wed Aug 03, 2022 7:28 pm
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"}

Re: How to get recipe icons of all recipes

Posted: Thu Aug 04, 2022 8:34 am
by M.Colcko
Just tried it, and it works just as I hoped it would. Thank you!