Page 1 of 1
Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 6:35 pm
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?
Re: Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 6:57 pm
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?
Re: Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 8:03 pm
by DRY411S
Klonan wrote:Why not just define both a normal and expensive recipe?
Normal and expensive reverse recipes?
Re: Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 8:14 pm
by Klonan
DRY411S wrote:Klonan wrote:Why not just define both a normal and expensive recipe?
Normal and expensive reverse recipes?
Sure
Re: Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 9:49 pm
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?
Re: Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 9:59 pm
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
},
},
Re: Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 10:08 pm
by DRY411S
Thanks that works if I can have an array of results instead of a singular result. I'll give it a try.
Re: Event for when expensive recipes are selected?
Posted: Thu Jun 01, 2017 10:18 pm
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.
Re: Event for when expensive recipes are selected?
Posted: Fri Jun 02, 2017 8:05 am
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.
Re: Event for when expensive recipes are selected?
Posted: Fri Jun 02, 2017 8:16 am
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.
Re: Event for when expensive recipes are selected?
Posted: Fri Jun 02, 2017 9:27 am
by bobingabout
I'm glad that you've figured it out then

Re: Event for when expensive recipes are selected?
Posted: Fri Jun 02, 2017 6:50 pm
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
Re: Event for when expensive recipes are selected?
Posted: Fri Jun 02, 2017 7:00 pm
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.
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.
Re: Event for when expensive recipes are selected?
Posted: Fri Jun 02, 2017 7:29 pm
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

, 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.
Re: Event for when expensive recipes are selected?
Posted: Fri Jun 02, 2017 7:44 pm
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

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