Copying recipes script

Place to get help with not working mods / modding interface.
Syriusz
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Thu May 07, 2015 12:34 pm
Contact:

Copying recipes script

Post by Syriusz »

Hello, I have problem with script that supposed to copy recipes that have less than 3 ingredients, then change them a little and add them to recipe list. I am out of ideas how to fix it... I do not have any previous experience with Lua, so probably its noob mistake or so. Here is code:

Code: Select all

 
  function tablelength(T)  ---it's to count how many ingredients is in recipe
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end


 for _, recipe in pairs(data.raw.recipe) do
	if recipe.category ~= "pneumatic" and recipe.category ~="smelting" then
		if tablelength(recipe.ingredients) < 3 then 
			local rec = recipe
			local energy = recipe.energy_required
			rec.name= recipe.name.."-P"
			rec.ingredients[3]={type="fluid", name="compressed-air", amount=energy}
			rec.category="pneumatic"
			data:extend(rec)
		end
    end
end
Thanks in advance for any help!
Rseding91
Factorio Staff
Factorio Staff
Posts: 15908
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Copying recipes script

Post by Rseding91 »

Try this:

Code: Select all

local excludedRecipeCategories = {pneumatic = true, smelting = true}

for k,recipe in pairs(data.raw.recipe) do
  if excludedRecipeCategories[recipe.category] == nil and #recipe.ingredients < 3 then
    local newRecipe = util.table.deepcopy(recipe)
    newRecipe.name = newRecipe.name .. "-P"
    newRecipe.ingredients[3] = {type="fluid", name="compressed-air", amount=newRecipe.energy_required}
    newRecipe.category = "pneumatic"
    data.raw.recipe[newRecipe.name] = newRecipe
  end
end
If you want to get ahold of me I'm almost always on Discord.
Syriusz
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Thu May 07, 2015 12:34 pm
Contact:

Re: Copying recipes script

Post by Syriusz »

It looks so much better than mine, thanks!
Some recipes does not have energy_required and I forgot about it, so it took me a while to repair it by adding:

Code: Select all

if newRecipe.ingredients[3].amount==nil then
newRecipe.ingredients[3].amount=0.5
end
It works now as I wanted, and I learned much from your code! Great thanks!
Post Reply

Return to “Modding help”