Page 1 of 1

[Solved] Arithmetic Error

Posted: Tue May 24, 2016 4:18 pm
by TheSAguy
Hi,

I use a function that doubles the ingredients of a recipe:

Code: Select all

	--- Rocket Part Cost Tweaks	
	for _, recipe in pairs({"low-density-structure", "rocket-fuel", "rocket-control-unit", "rocket-part", "satellite"}) do
		for _, ingredient in pairs(data.raw.recipe[recipe].ingredients) do
			ingredient[2] = ingredient[2] * 2
		end
	end
Every now ant then I get an 'Arithmetic Error':
Image

This happens when there is another Mod that also touches that recipe.

First off, I'm not exactly sure what that error means.
Second, how do I prevent it.

What's more frustrating, when I was trying to find a fix, I started to get the same error with a process that was working.
I was tweaking the cost of Bob's Science Pack 4:

Code: Select all

	if data.raw.recipe["science-pack-4"] ~= nil then
		for _, recipe in pairs({"science-pack-4"}) do
			for _, ingredient in pairs(data.raw.recipe[recipe].ingredients) do
				ingredient[2] = ingredient[2] * 2
			end
		end
	end
Any ideas on what this error is and how I can prevent it?
I've tried to do a validation check first:

Code: Select all

if data.raw.recipe["blah blah"] then
though valid, the error still happens.

Thanks,.

Re: Arithmetic Error

Posted: Tue May 24, 2016 4:25 pm
by prg
If ingredient[2] = ingredient[2] * 2 blows up with that error, it means ingredient[2] is nil. You must be encountering a recipe like

Code: Select all

  {
    type = "recipe",
    name = "express-transport-belt",
    category = "crafting-with-fluid",
    enabled = false,
    ingredients =
    {
      {"iron-gear-wheel", 5},
      {"fast-transport-belt", 1},
      {type="fluid", name="lubricant", amount=2},
    },
    result = "express-transport-belt"
  },
So do a check if ingredient[2] is nil, and if so use ingredient.amount instead.

Re: Arithmetic Error

Posted: Tue May 24, 2016 5:16 pm
by TheSAguy
Is this how it will look:

Code: Select all

   if data.raw.recipe["science-pack-4"] ~= nil then
		for _, recipe in pairs({"science-pack-4"}) do
			for _, ingredient in pairs(data.raw.recipe[recipe].ingredients) do
				if ingredient[2] ~= nil then
					ingredient[2] = ingredient[2] * 2
				else
					ingredient.amount = ingredient.amount * 2
				end	
			end
		end
   end

Re: Arithmetic Error

Posted: Tue May 24, 2016 8:41 pm
by prg
TheSAguy wrote:Is this how it will look:

Code: Select all

   if data.raw.recipe["science-pack-4"] ~= nil then
		for _, recipe in pairs({"science-pack-4"}) do
			for _, ingredient in pairs(data.raw.recipe[recipe].ingredients) do
				if ingredient[2] ~= nil then
					ingredient[2] = ingredient[2] * 2
				else
					ingredient.amount = ingredient.amount * 2
				end	
			end
		end
   end
I don't understand such posts. Just try it and see if it does what you want. If it doesn't and you're having trouble figuring it out, ask a concrete question.

Re: Arithmetic Error

Posted: Tue May 24, 2016 9:20 pm
by TheSAguy
no offence intended, was at work and could not test.
It appears that the above is working. Thanks for your guidance!

Re: Arithmetic Error

Posted: Tue May 24, 2016 10:10 pm
by prg
Not offended, just wondering why you would ask on the forum if something would work if you did it, having to wait half a day for an answer instead of simply trying it yourself. Ah well. Glad it works. You're welcome.

Re: [Solved] Arithmetic Error

Posted: Thu May 26, 2016 9:45 am
by bobingabout
Bob's library actually assumes the other way around.

it checks if recipe.amout exists (or in certain cases on results, amount_min and amount_max) and if they don't exist, it uses recipe[2]. I should probably check if that exists too, you know, as a safety measure.