How to reference base Prototype fields for building other prototypes

Place to get help with not working mods / modding interface.
Post Reply
johnnyjigglez
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri May 20, 2022 2:04 am
Contact:

How to reference base Prototype fields for building other prototypes

Post 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.

User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

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

Post 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

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

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

Post 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

johnnyjigglez
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri May 20, 2022 2:04 am
Contact:

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

Post 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.

johnnyjigglez
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri May 20, 2022 2:04 am
Contact:

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

Post 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...

johnnyjigglez
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri May 20, 2022 2:04 am
Contact:

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

Post by johnnyjigglez »

I've also attempted to establish my "foo" reference in my mod's data-final-fixes.lua to no avail.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2530
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

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

Post by FuryoftheStars »

Between the 2 lines of code, which one is it actually erroring on (as both contain arithmetic operations)?
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

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

Post 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"]))

johnnyjigglez
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri May 20, 2022 2:04 am
Contact:

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

Post by johnnyjigglez »

data.raw.recipe["iron-plate"].energy_required
presents the nil

robot256
Filter Inserter
Filter Inserter
Posts: 596
Joined: Sun Mar 17, 2019 1:52 am
Contact:

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

Post 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.

johnnyjigglez
Burner Inserter
Burner Inserter
Posts: 6
Joined: Fri May 20, 2022 2:04 am
Contact:

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

Post 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

Post Reply

Return to “Modding help”