for _, recipe in pairs({"science-pack-1", "science-pack-2", "science-pack-3", "alien-science-pack"}) do
for _, ingredient in pairs(data.raw.recipe[recipe].ingredients) do
ingredient[2] = ingredient[2] * 2
end
end
but for my case i get the error for the line with the multiplication: "attempt to perform arithmetic on field '?' (a nil value)".
I found out that it works for some ingredients if i do it like this:
for _, recipe in pairs({"solar-panel", "solar-panel-small", "solar-panel-large", "solar-panel-2", "solar-panel-small-2", "solar-panel-large-2", "solar-panel-3", "solar-panel-small-3", "solar-panel-large-3"}) do
for _, ingredient in pairs(data.raw.recipe[recipe].ingredients) do
if ingredient[1] == "solar-panel" then
ingredient[2] = ingredient[2] * 2
end
end
end
Which works for solar panels and the basic ingredients in the recipe, but not for some of the ingredients changed out by bobspower. Bobs exchanges steel to titanium plate for example for certain panels. Even if i do
if ingredient[2] ~= nil then
ingredient[2] = ingredient[2] * 2
end
to get rid of the error, the switched out ingredients don't get multiplied for some reason. It gets better though: Some ingredients, that do get exchanged (like circuit boards) will get multiplied, but not titanium / silver plates etc. Also, if i switch from multiplying by 2 to dividing by 2, i get an error: "conversion of data to type "unsigned short" failed" so even if the first issue gets sorted out it looks like i can't divide anyway? (there are a few ingredients with odd numbers which might be the problem)
I looked at the code from bobslibrary but i'm not really versed in lua so i can't judge if titanium plates etc not getting multiplied is an issue with how it changes out the ingredients. The code is here in any case:
function bobmods.lib.recipe.replace_ingredient(recipe, old, new)
if data.raw.recipe[recipe] and bobmods.lib.item.get_type(new) then
local amount = 0
for i, ingredient in pairs(data.raw.recipe[recipe].ingredients) do
if ingredient[1] == old then
amount = ingredient[2] + amount
end
if ingredient.name == old then
amount = ingredient.amount + amount
end
end
if amount > 0 then
bobmods.lib.recipe.remove_ingredient(recipe, old)
bobmods.lib.recipe.add_ingredient(recipe, {new, amount})
return true
else
return false
end
else
if not data.raw.recipe[recipe] then
log("Recipe " .. recipe .. " does not exist.")
end
if not bobmods.lib.item.get_type(new) then
log("Ingredient " .. new .. " does not exist.")
end
return false
end
end
which calls remove_ingredient and add_ingredient which both call on yet another function, remove and add
function bobmods.lib.recipe.remove_ingredient(recipe, item)
if data.raw.recipe[recipe] then
bobmods.lib.item.remove(data.raw.recipe[recipe].ingredients, item)
else
log("Recipe " .. recipe .. " does not exist.")
end
end
function bobmods.lib.recipe.add_ingredient(recipe, item)
if data.raw.recipe[recipe] and bobmods.lib.item.get_type(bobmods.lib.item.basic_item(item).name) then
bobmods.lib.item.add(data.raw.recipe[recipe].ingredients, bobmods.lib.item.basic_item(item))
else
if not data.raw.recipe[recipe] then
log("Recipe " .. recipe .. " does not exist.")
end
if not bobmods.lib.item.get_basic_type(bobmods.lib.item.basic_item(item).name) then
log("Ingredient " .. bobmods.lib.item.basic_item(item).name .. " does not exist.")
end
end
end
function bobmods.lib.item.remove(list, item)
for i, object in ipairs(list) do
if object[1] == item or object.name == item then
table.remove(list, i)
end
end
end
function bobmods.lib.item.add(list, item_in) --increments amount if exists
local item = bobmods.lib.item.item(item_in)
local addit = true
for i, object in pairs(list) do
if object[1] == item.name or object.name == item.name then
addit = false
list[i] = bobmods.lib.item.combine(object, item)
end
end
if addit then table.insert(list, item) end
end
Ah, that explains why it only affected some. Still not sure how i would go about fixing that since it's basically inside a string and i'd have to multiply just the number...
And then there's the error when dividing too.
Maybe i should just change everything manually.
Re: Multiply / divide recipe ingredients
Posted: Fri Feb 17, 2017 5:49 pm
by prg
You just use ingredient.amount instead of ingredient[2] in the second case.
Try math.floor() for the conversion problem.
Re: Multiply / divide recipe ingredients
Posted: Fri Feb 17, 2017 5:57 pm
by Kenira
Thank you very much, it works now!
I had even tried the amount before but because not every recipe had it i of course got an error, now i just try both with a ~= nil.
Re: Multiply / divide recipe ingredients
Posted: Fri Feb 17, 2017 5:57 pm
by daniel34
I once wrote this function, it should still work and multiplies every ingredient (item or fluid) of the given recipe with a float value (e.g. 0.5 to divide by 2). Item results are rounded, items that would require < 0.5 are rounded up to 1 instead of being removed.
EDIT: I see you already got it in the time it took me to find it , but incase someone else needs it:
function round_ingredient(num)
if num < 1 then
return 1;
else
return math.floor(num+0.5);
end
end
function rescale_recipe(recipe, factor)
for _, ingredient in pairs(recipe.ingredients) do
if ingredient[2] ~= nil then
ingredient[2] = round_ingredient(ingredient[2] * factor);
end
--log("recipe: "..recipe.name.." type: "..ingredient["type"]);
if ingredient["amount"] ~= nil then
if ingredient["type"] == "fluid" then
ingredient["amount"] = ingredient["amount"] * factor;
else
ingredient["amount"] = round_ingredient(ingredient["amount"] * factor);
end
end
end
end
Re: Multiply / divide recipe ingredients
Posted: Fri Feb 17, 2017 6:06 pm
by Kenira
Still a big thanks to you too!
I was also just discovering that i do need to catch the case where 1 / 2 becomes 0 ^^
Re: Multiply / divide recipe ingredients
Posted: Fri Feb 17, 2017 6:56 pm
by Klonan
Kenira wrote:Still a big thanks to you too!
I was also just discovering that i do need to catch the case where 1 / 2 becomes 0 ^^