Page 1 of 1

Change a recipe on event

Posted: Fri Jun 07, 2019 8:42 am
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
)

Re: Change a recipe on event

Posted: Fri Jun 07, 2019 10:05 am
by darkfrei
You can not do this.
Data stage is earlier than control stage.
https://lua-api.factorio.com/latest/Data-Lifecycle.html

Re: Change a recipe on event

Posted: Fri Jun 07, 2019 10:25 am
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

Re: Change a recipe on event

Posted: Fri Jun 07, 2019 10:43 am
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.