Page 1 of 1

How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 3:27 am
by johnnyjigglez
How does one go about referencing the fields of prototypes found in the base game during the Data stage?

Sample:

Code: Select all

local foo = 5 * data.raw.recipe["iron-plate"].energy_required
data.raw.recipe["my-bar"].energy_required = foo + 16

The game errors on bootup stating that i cannot use arithmetic on a nil valued field.

Maybe I'm not quite understanding fully how the data loading works.

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 3:34 am
by ptx0
johnnyjigglez wrote:
Fri May 20, 2022 3:27 am
How does one go about referencing the fields of prototypes found in the base game during the Data stage?

Sample:

Code: Select all

local foo = 5 * data.raw.recipe["iron-plate"].energy_required
data.raw.recipe["my-bar"].energy_required = foo + 16

The game errors on bootup stating that i cannot use arithmetic on a nil valued field.

Maybe I'm not quite understanding fully how the data loading works.
you first need to define the recipe

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 4:35 am
by DaveMcW
energy_required is an optional field which is allowed to be nil during the data stage. If it is still nil at the end, it gets set to the default value of 0.5.

This means you need to check for nil before doing math on it.

Code: Select all

local energy = data.raw.recipe["iron-plate"].energy_required or 0.5
data.raw.recipe["my-bar"].energy_required = 5 * energy + 16

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 5:53 am
by johnnyjigglez
according to data\base\prototypes\recipe.lua:

Code: Select all

  {
    type = "recipe",
    name = "iron-plate",
    category = "smelting",
    energy_required = 3.2,
    ingredients = {{"iron-ore", 1}},
    result = "iron-plate"
  },
I'm attempting to refer to this prototype and specifically the field noted, energy_required.

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 5:54 am
by johnnyjigglez
DaveMcW wrote:
Fri May 20, 2022 4:35 am
energy_required is an optional field which is allowed to be nil during the data stage. If it is still nil at the end, it gets set to the default value of 0.5.

This means you need to check for nil before doing math on it.

Code: Select all

local energy = data.raw.recipe["iron-plate"].energy_required or 0.5
data.raw.recipe["my-bar"].energy_required = 5 * energy + 16
According to the included base "mod" recipe.lua, the field is populated...

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 5:56 am
by johnnyjigglez
I've also attempted to establish my "foo" reference in my mod's data-final-fixes.lua to no avail.

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 1:46 pm
by FuryoftheStars
Between the 2 lines of code, which one is it actually erroring on (as both contain arithmetic operations)?

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 2:52 pm
by DaveMcW
You can use log() and serpent.block() to find the nil. This outputs to factorio-current.log

Code: Select all

log(serpent.block(data.raw.recipe["iron-plate"]))
log(serpent.block(data.raw.recipe["my-bar"]))

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 7:02 pm
by johnnyjigglez
data.raw.recipe["iron-plate"].energy_required
presents the nil

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 7:42 pm
by robot256
Start a game and press ctrl-shift-E for the prototype explorer. You can see the actual value it is using, which should be the 0.5 default value in that case.

Re: How to reference base Prototype fields for building other prototypes

Posted: Fri May 20, 2022 8:53 pm
by johnnyjigglez
Thank you, using the log and serpent I was able to update my script more defensively... Taking into account that fields can be nested in difficulty blocks.

My issue has been resolved.

I'm patching an existing mod around furnaces such that craft speed across several tiers of furnaces take into account all smelting recipes and their time-to-smelt as well as the vanilla electric furnace's electrical consumption to craft.

think of it as a mod's mod