Change a recipe on event

Place to get help with not working mods / modding interface.
irpepper
Manual Inserter
Manual Inserter
Posts: 1
Joined: Fri Jun 07, 2019 8:39 am
Contact:

Change a recipe on event

Post by irpepper »

Hi everyone, I'm attempting to write a mod that will reduce the crafting time of items the more you make them. I'm trying to edit the recipe energy_required everytime the item is finished being crafted, but this doesn't seem to work. If you could point me in the right direction it would be greatly appreciated!

Code: Select all

local function reduce_energy_required(recipe)
    data.raw["recipe"][recipe.name].energy = data.raw["recipe"][recipe.name].energy_requrired * 0.999
 end

script.on_event({defines.events.on_player_crafted_item},
   function (e)
      reduce_energy_required(e.recipe)
   end
)
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Change a recipe on event

Post by darkfrei »

You can not do this.
Data stage is earlier than control stage.
https://lua-api.factorio.com/latest/Data-Lifecycle.html
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Change a recipe on event

Post by eradicator »

You'd have to generate a bunch of recipes in data stage. Then switch them out dynamically.

Btw, the player uses the same recipes as machines do. Do you want to make all machines faster every time the player crafts an item?

Also fun fact:
(0.5*60)*0.999^3400 ≊ 1
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Qon
Smart Inserter
Smart Inserter
Posts: 2164
Joined: Thu Mar 17, 2016 6:27 am
Contact:

Re: Change a recipe on event

Post by Qon »

irpepper wrote: Fri Jun 07, 2019 8:42 am

Code: Select all

r.energy = r.energy_requrired * 0.999
Even if it was possible, your code wouldn't work anyways.

It's energy_required, not energy and not energy_requrired.
My mods: Capsule Ammo | HandyHands - Automatic handcrafting | ChunkyChunks - Configurable Gridlines
Some other creations: Combinassembly Language GitHub w instructions and link to run it in your browser | 0~drain Laser
Post Reply

Return to “Modding help”