Bug or feature? Strings accepted instead of numbers in Types/*ProductPrototype

Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1659
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Bug or feature? Strings accepted instead of numbers in Types/*ProductPrototype

Post by Pi-C »

I've got a crash report for one of my mods:

Code: Select all

7.436 Error ModManager.cpp:1623: Failed to load mod "GCKI": __GCKI__/libs/recipe-functions.lua:1111: attempt to compare number with string
The crash happened when the last of the following results:

Code: Select all

   7.443 Script @__GCKI__/libs/debugging.lua:159: results: {
  {
    amount = 1,
    name = "nullius-box-phosphorus",
    type = "item"
  },
  {
    amount = 12,
    name = "nullius-box-gravel",
    type = "item"
  },
  {
    amount = "120",	-- String instead of number!
    name = "nullius-carbon-monoxide",
    type = "fluid"
  }
}
was passed on to my function:

Code: Select all

  for r, result in ipairs(results or {}) do
      amount = result[2] or result.amount
      if amount and amount > 0 then	-- CRASHING 
    	…
According to the description of the property amount of Types/FluidProductType, there's nothing wrong with me assuming that amount will always be a number, so I've forwarded the crash report to the author of the mod that created the faulty recipe. But then I started wondering why nobody has noticed before that there was something wrong with that recipe.

I removed my mod, restarted the game -- and sure enough, the recipe was there, and it also created the expected fluid result! So, if Types/FluidProductPrototype.amount is a string that can be converted to a number, the game will accept it.

I've dug around some more and found that this also applies to the properties amount_min, amount_max, and catalyst_amount (both Types/ItemProductType and Types/FluidProductType) as well as the property temperature (only Types/FluidProductType).
Changing recipe.results to

Code: Select all

    results = {
      {"nullius-box-phosphorus", 1},
      -- {"nullius-box-gravel", 12},
      {type = "item", name = "nullius-box-gravel", amount_min = "12", amount_max = "15", catalyst_amount = "7"},
      -- {type="fluid", name="nullius-carbon-monoxide", amount = "120"}
      {type="fluid", name="nullius-carbon-monoxide", amount_min = "120", amount_max = "123", temperature = "66.6", catalyst_amount = "5"}
    },
will show this in the game:
recipe.png
recipe.png (1.3 MiB) Viewed 784 times
For both product types, this also applies to the property probability if the string starts with a number. For example, changing the last result to

Code: Select all

      {type="fluid", name="nullius-carbon-monoxide", amount_min = "120", amount_max = "123", temperature = "66.6", catalyst_amount = "5", probability = "0.5"}
will give you this:
recipe_in_game.png
recipe_in_game.png (55.36 KiB) Viewed 784 times
However, changing probability to

Code: Select all

-- probability = "0.5"
probability = ".5"
will result in

Code: Select all

Error ModManager.cpp:1623: Error while loading recipe prototype "lambent-nil-boxed-phosphorus-1" (recipe): Difficulty normal: Value must be a number in property tree at ROOT.recipe.lambent-nil-boxed-phosphorus-1.results[2].probability
I'd consider the current behavior rather a bug than a feature because it will only work for strings formatted in a particular way (leading zeros are not optional) and because this behavior isn't documented (so mods which don't take into account that strings translating to numbers are valid may crash). But I must admit that the game trying to convert strings to numbers automatically is convenient and could be a feature.

My recommendation: Keep the current behavior (but fix that the first character of a number-strings may not be a decimal point) and update the documentation.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Rseding91
Factorio Staff
Factorio Staff
Posts: 13246
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Bug or feature? Strings accepted instead of numbers in Types/*ProductPrototype

Post by Rseding91 »

The behavior is legacy from long ago. In 1.2 it will be removed and require all of the values be numbers.
If you want to get ahold of me I'm almost always on Discord.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1659
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Bug or feature? Strings accepted instead of numbers in Types/*ProductPrototype

Post by Pi-C »

Thanks for the info! :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Fixed for 2.0”