When a player crafts an item the game triggers the event "on_player_crafted_item". In that even you get the variable item_stack. So, you have the items name with "event.item_stack.name". How does one get the recipe class of the achieved name.
I tried a couple of things that (of course) didn't work. I tried just adding ".recipe" after the "item_stack" (with and without ".name"). and some other things that didn't work.
How would one get the recipe of an item if you have the name of that item?
thanks,
Matjojo
recipe by name
Re: recipe by name
You need to search player.force.recipes for a matching item.
Note that it's possible for an item to have more than one recipe.
Code: Select all
function get_recipe(player, item)
for _, recipe in pairs(player.force.recipes) do
for _, product in pairs(recipe.products) do
if (product.name == item) then
return recipe.name
end
end
end
return nil
end
Re: recipe by name
Thanks, that worked. I figured I needed to call the function like this:DaveMcW wrote:You need to search player.force.recipes for a matching item.snippy snippet
Code: Select all
get_recipe(game.player, event.item_stack.name)
now, you pointed out that there are items with more then one recipe, would you know a way to make sure you have the right recipe selected?