Need help replace items in all recipes

Place to get help with not working mods / modding interface.
Post Reply
BOFan80
Inserter
Inserter
Posts: 23
Joined: Thu Apr 21, 2016 6:44 pm
Contact:

Need help replace items in all recipes

Post 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.
please notice that my natove language is german, so my english is not so good. Thx

Qon
Smart Inserter
Smart Inserter
Posts: 2119
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Need help replace items in all recipes

Post 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.

BOFan80
Inserter
Inserter
Posts: 23
Joined: Thu Apr 21, 2016 6:44 pm
Contact:

Re: Need help replace items in all recipes

Post 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?
please notice that my natove language is german, so my english is not so good. Thx

Pi-C
Smart Inserter
Smart Inserter
Posts: 1648
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Need help replace items in all recipes

Post 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. :mrgreen:

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")
Last edited by Pi-C on Mon Apr 18, 2022 3:24 pm, edited 1 time in total.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

BOFan80
Inserter
Inserter
Posts: 23
Joined: Thu Apr 21, 2016 6:44 pm
Contact:

Re: Need help replace items in all recipes

Post by BOFan80 »

Thx u help me lot
please notice that my natove language is german, so my english is not so good. Thx

Post Reply

Return to “Modding help”