Page 1 of 1

recipe.ingredients

Posted: Sun Jun 18, 2017 3:26 am
by sore68
Hi I have a some problem.

I add a new science pack recipes, and add new ingredients array with table.insert.

But this is change original science pack recipes
001.png
001.png (91.17 KiB) Viewed 1605 times

Code: Select all

local item_name = "iron-plate"

local scipack_container = {"science-pack-1", "science-pack-2", "science-pack-3", "military-science-pack", "production-science-pack", "high-tech-science-pack"}

for _, recipe in pairs (data.raw["recipe"]) do
  if string.find(recipe.name, "-pack") then
    for i = 1, #scipack_container do
      if recipe.name == scipack_container[i] then
        data:extend({
          sp_recipe{
            name = "AA_"..recipe.name,
            energy_required = recipe.energy_required * 2,
            order = i.."-alien-science-"..scipack_container[i],
            icon = "__Additional-Alien__/graphics/icon/"..recipe.name..".png",
            ingredients = recipe.ingredients,
            results = {{type = "item", name = recipe.name, amount = 3}}
          }
        })
        table.insert(data.raw.recipe["AA_"..recipe.name].ingredients, 1, {item_name, i*5})
        -- table.insert(data.raw.recipe[scipack_container[i]].ingredients, 1, {item_name, i*5})
        -- table.remove(data.raw.recipe[scipack_container[i]].ingredients, 1)
      end
    end
  end
end

Could you tell me why the problem occurred??

Re: recipe.ingredients

Posted: Sun Jun 18, 2017 1:15 pm
by Choumiko
If i understand it right you want to add new recipes for the existing science, that require an additinal ingredient but produce more of the science pack?

In that case:

Code: Select all

require 'util'
local item_name = "iron-plate"
local scipack_container = {"science-pack-1", "science-pack-2", "science-pack-3", "military-science-pack", "production-science-pack", "high-tech-science-pack"}

for i, name in pairs(scipack_container) do
    local recipe = data.raw.recipe[name]
    data:extend({
        {
            type = "recipe",
            name = "AA_"..recipe.name,
            energy_required = recipe.energy_required * 2,
            order = i.."-alien-science-"..scipack_container[i],
            --icon = "__Additional-Alien__/graphics/icon/"..recipe.name..".png",
            ingredients = table.deepcopy(recipe.ingredients),
            results = {{type = "item", name = recipe.name, amount = 3}}
        }
    })
    table.insert(data.raw.recipe["AA_"..recipe.name].ingredients, 1, {item_name, i*5})
end
should work (untested). The new recipes will be available from the start though, you'll have to modifiy the technologies that unlock them to also unlock your recipes.
The problem with your code was that it modified a reference to the original tables, table.deepcopy solves that.

Re: recipe.ingredients

Posted: Sun Jun 18, 2017 1:53 pm
by sore68
Choumiko wrote: ...

Wow!
Now it works just as I intended!
thank you so love Choumiko :D
001.png
001.png (312.89 KiB) Viewed 1575 times