Page 1 of 1
Need help replace items in all recipes
Posted: Mon Apr 18, 2022 9:42 am
by BOFan80
I need your help, i waant to replace all ironplates in recipes with an item who called carbon-plate.
i tried different solutions, wich i found, but no ones helps.
I tried to wrote a script but there is an error. pls help me
script is saved in control.lua
Code: Select all
script.on_init
(function() ReplaceAllIngredientItemWithItem(iron-plate, carbon-plate)
for i, recipe in pairs(data.raw["recipe"]) do
for v, ingredient in pairs(data.raw["recipe"][recipe.name].ingredients) do
if ingredient.name == iron-plate then
RemoveFromRecipe(recipe.name,iron-plate)
AddToRecipe(recipe.name, carbon-plate, ingredient.amount)
elseif ingredient[1] == iron-plate then
RemoveFromRecipe(recipe.name,iron-plate)
AddToRecipe(recipe.name, carbon-plate, ingredient[2])
end
end
end)
ore is there a solution without script.
Re: Need help replace items in all recipes
Posted: Mon Apr 18, 2022 11:04 am
by Qon
Variable "data" is available at data stage, in data.lua, not in control stage in control.lua.
Also, "script" is only available in control stage. Take the code inside the function and just execute it directly.
Re: Need help replace items in all recipes
Posted: Mon Apr 18, 2022 11:26 am
by BOFan80
Qon wrote: Mon Apr 18, 2022 11:04 am
Variable "data" is available at data stage, in data.lua,
not in control stage in control.lua.
Also, "script" is only available in control stage. Take the code inside the function and just execute it directly.
Thx
i copy this
Code: Select all
function ReplaceAllIngredientItemWithItem (iron-plate , carbon-plate)
for i, recipe in pairs(data.raw["recipe"]) do
for v, ingredient in pairs(data.raw["recipe"][recipe.name].ingredients) do
if ingredient.name == iron-plate then
RemoveFromRecipe(recipe.name,iron-plate)
AddToRecipe(recipe.name, carbon-plate, ingredient.amount)
elseif ingredient[1] == iron-plate then
RemoveFromRecipe(recipe.name,iron-plate)
AddToRecipe(recipe.name, carbon-plate, ingredient[2])
end
end
end
in to my data.lua. but it shows allways an error
did i forget something?
Re: Need help replace items in all recipes
Posted: Mon Apr 18, 2022 2:28 pm
by Pi-C
BOFan80 wrote: Mon Apr 18, 2022 11:26 am
did i forget something?
Handling recipes is something just short of dark magic.
There are different places in a recipe where ingredients, results etc. can be set. In the most simple case, it will be at the root, like recipe.ingredients. But a recipe could have difficulty (IIRC, if there is at least one difficulty defined, that will be used at the end of the data stage, and the values at the root level will be discarded). So you must check recipe.normal.ingredients and recipe.expensive.ingredients as well.
Also, ingredients can be in either
short or long format, so you must check each ingredient for both variants.
This should work:
Code: Select all
local function replace_in_difficulty(ingredients, old, new)
if not (ingredients and old and new) then
return
end
for i, ingredient in pairs(ingredients) do
if ingredient[1] == old then
ingredient[1] = new
elseif ingredient.name == old then
ingredient.name = new
end
end
end
function ReplaceAllIngredientItemWithItem(old, new)
for i, recipe in pairs(data.raw.recipe) do
replace_in_difficulty(recipe.ingredients, old, new)
replace_in_difficulty(recipe.normal and recipe.normal.ingredients, old, new)
replace_in_difficulty(recipe.expensive and recipe.expensive.ingredients, old, new)
end
end
ReplaceAllIngredientItemWithItem("iron-plate", "carbon-plate")
Re: Need help replace items in all recipes
Posted: Mon Apr 18, 2022 3:05 pm
by BOFan80
Thx u help me lot