Event for when expensive recipes are selected?

Place to get help with not working mods / modding interface.
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Event for when expensive recipes are selected?

Post by DRY411S »

So my Recycling Machines Mod https://mods.factorio.com/mods/DRY411S/ZRecycling works by loading all the vanilla and installed mods recipes and reverses them by turning the original results into ingredients and the original ingredients into results.

It does this in the game data stage when the game is initially started by players.It has to use the data stage because the recipes form part of data.raw.

The problem is that at that stage there are normal ingredients and expensive ingredients, and I have to choose one or the other as the results of the recycling recipes. I'm defaulting to the normal ingredients at the moment.

Is there an event I can trap in control.lua that would allow me to alter the ingredients in data.raw when people start a new map, and decide to use expensive ingredients?
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5412
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Event for when expensive recipes are selected?

Post by Klonan »

DRY411S wrote:So my Recycling Machines Mod https://mods.factorio.com/mods/DRY411S/ZRecycling works by loading all the vanilla and installed mods recipes and reverses them by turning the original results into ingredients and the original ingredients into results.

It does this in the game data stage when the game is initially started by players.It has to use the data stage because the recipes form part of data.raw.

The problem is that at that stage there are normal ingredients and expensive ingredients, and I have to choose one or the other as the results of the recycling recipes. I'm defaulting to the normal ingredients at the moment.

Is there an event I can trap in control.lua that would allow me to alter the ingredients in data.raw when people start a new map, and decide to use expensive ingredients?
Why not just define both a normal and expensive recipe?
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Event for when expensive recipes are selected?

Post by DRY411S »

Klonan wrote:Why not just define both a normal and expensive recipe?
Normal and expensive reverse recipes?
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5412
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Event for when expensive recipes are selected?

Post by Klonan »

DRY411S wrote:
Klonan wrote:Why not just define both a normal and expensive recipe?
Normal and expensive reverse recipes?
Sure
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Event for when expensive recipes are selected?

Post by DRY411S »

Klonan wrote:
DRY411S wrote:
Klonan wrote:Why not just define both a normal and expensive recipe?
Normal and expensive reverse recipes?
Sure
So is there a variable I can test to see whether the player is using expensive recipes?
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Event for when expensive recipes are selected?

Post by orzelek »

DRY411S wrote:
Klonan wrote:
DRY411S wrote:
Klonan wrote:Why not just define both a normal and expensive recipe?
Normal and expensive reverse recipes?
Sure
So is there a variable I can test to see whether the player is using expensive recipes?
I think you are not realizing something.
Take a look at base game data - some recipes (like electronics) have two sections in recipe - normal and expensive.
So your reversed recipe needs to be done in same way. You need to reverse normal section into your normal section and same with expensive section. Then game will select correct one based on the setting.
Like this one:

Code: Select all

  {
    type = "recipe",
    name = "electronic-circuit",
    normal =
    {
      ingredients =
      {
        {"iron-plate", 1},
        {"copper-cable", 3}
      },
      result = "electronic-circuit",
      requester_paste_multiplier = 50
    },
    expensive =
    {
      ingredients =
      {
        {"iron-plate", 2},
        {"copper-cable", 10}
      },
      result = "electronic-circuit",
      requester_paste_multiplier = 50
    },
  }, 
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Event for when expensive recipes are selected?

Post by DRY411S »

Thanks that works if I can have an array of results instead of a singular result. I'll give it a try.
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Event for when expensive recipes are selected?

Post by Ranakastrasz »

DRY411S wrote:Thanks that works if I can have an array of results instead of a singular result. I'll give it a try.
I think you can put the result in the easy/hard crafting suitable. At least I hope so.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Event for when expensive recipes are selected?

Post by bobingabout »

I don't even understand what the issue is.

if normal exists, then expensive exists too.

do a check to see if these tags exist, if they don't, business as usual.

if they do, simply do what you normally do, except you do the recipe reversing part twice, inside both normal, and expensive. I mean, Both the ingredients and result/results tags exist inside normal/expensive, just the same as outside of them.

it literally is that simple.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Event for when expensive recipes are selected?

Post by DRY411S »

bobingabout wrote:I don't even understand what the issue is.
You (and the others above) are right. There isn't an issue, I was having a brain meltdown, thinking that my reverse recipes must be either normal or expensive, when in fact they can be both.

Thanks for steering me on the right course folks.
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Event for when expensive recipes are selected?

Post by bobingabout »

I'm glad that you've figured it out then :D
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
Exasperation
Long Handed Inserter
Long Handed Inserter
Posts: 88
Joined: Fri Apr 22, 2016 9:55 pm
Contact:

Re: Event for when expensive recipes are selected?

Post by Exasperation »

bobingabout wrote:if normal exists, then expensive exists too.
A word of warning, this is apparently not strictly true. I recently had a request to change some of my code to make it compatible with a mod that was creating recipes with expensive but not normal variants. As a result, I had to move to something like this:

Code: Select all

   if recipe.normal or recipe.expensive then
      if recipe.normal then
         --do stuff with normal version
      end
      if recipe.expensive then
         --do stuff with expensive version
      end
   else
      --do stuff with base version
   end
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Event for when expensive recipes are selected?

Post by DRY411S »

Exasperation wrote:
bobingabout wrote:if normal exists, then expensive exists too.
A word of warning, this is apparently not strictly true. I recently had a request to change some of my code to make it compatible with a mod that was creating recipes with expensive but not normal variants. As a result, I had to move to something like this:

Code: Select all

   if recipe.normal or recipe.expensive then
      if recipe.normal then
         --do stuff with normal version
      end
      if recipe.expensive then
         --do stuff with expensive version
      end
   else
      --do stuff with base version
   end
Your username is ace in these circumstances. :D

So the mod that had expensive recipes only? What happened when people generated maps with normal recipes? Which mod was it?

(If the mod has only expensive recipes, I'd have pushed back on the mod developer and requested that they had just recipe.ingredients ~= nil instead)

I have inspected all vanilla recipes now, and they have recipe.ingredients ~= nil OR (recipe.normal.ingredients ~= nil AND recipe.expensive.ingredients ~= nil)

I've released a new version of my mod where the code assumes this.
Exasperation
Long Handed Inserter
Long Handed Inserter
Posts: 88
Joined: Fri Apr 22, 2016 9:55 pm
Contact:

Re: Event for when expensive recipes are selected?

Post by Exasperation »

I don't remember exactly which mod it was, but it was some marathon-style mod. Given that, I suppose "all my users are going to use expensive recipes" was a reasonable assumption on their part. I honestly didn't test to see what happened if a recipe only had an expensive version but the difficulty was set to normal (or vice versa), I just did one of these :roll:, made the change to avoid my code crashing from attempting to index a nil, and let it be their problem to deal with from then on.

And yes, all of the vanilla recipes either have both normal + expensive or neither.
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Event for when expensive recipes are selected?

Post by DRY411S »

Exasperation wrote:I don't remember exactly which mod it was, but it was some marathon-style mod. Given that, I suppose "all my users are going to use expensive recipes" was a reasonable assumption on their part. I honestly didn't test to see what happened if a recipe only had an expensive version but the difficulty was set to normal (or vice versa), I just did one of these :roll:, made the change to avoid my code crashing from attempting to index a nil, and let it be their problem to deal with from then on.

And yes, all of the vanilla recipes either have both normal + expensive or neither.
Well I guess if the mod writer had only one recipe, they should just ignore the normal and expensive constructs.
Post Reply

Return to “Modding help”