[Solved] Arithmetic Error

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Solved] Arithmetic Error

Post 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,.
Last edited by TheSAguy on Wed May 25, 2016 8:05 pm, edited 1 time in total.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Arithmetic Error

Post 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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Arithmetic Error

Post 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
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Arithmetic Error

Post 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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Arithmetic Error

Post by TheSAguy »

no offence intended, was at work and could not test.
It appears that the above is working. Thanks for your guidance!
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Arithmetic Error

Post 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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: [Solved] Arithmetic Error

Post 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.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
Post Reply

Return to “Modding help”