Adding recipes in control.lua
Adding recipes in control.lua
So my mod is for recycling. Every time something new finishes being researched, in control.lua I have a function that handles the defines.events.on_research_finished event to get the newly researched recipe from game.player.force.recipes[].
Then I build a local 'reversed' recipe (reverse ingredients and results). I want to use the reversed recipes in my recycling machine.
BUT I cannot seem to add my new recipe into the existing recipe list.
table.insert(data.raw.recipe,new_recipe) gives an error about global 'data' having a nil value
data:extend({new_recipe}) gives a similar error
table.insert(game.player.force.recipes,new_recipe) does not give an error, but my recipe does not appear to have been added (maybe because game.player.force.recipes is read-only?)
Can anybody show me where I am going wrong please.
(I know my recycling machines work if I manually add some reversed recipes in my mod's recipe.lua file, so it's not the machine that is broken!)
Then I build a local 'reversed' recipe (reverse ingredients and results). I want to use the reversed recipes in my recycling machine.
BUT I cannot seem to add my new recipe into the existing recipe list.
table.insert(data.raw.recipe,new_recipe) gives an error about global 'data' having a nil value
data:extend({new_recipe}) gives a similar error
table.insert(game.player.force.recipes,new_recipe) does not give an error, but my recipe does not appear to have been added (maybe because game.player.force.recipes is read-only?)
Can anybody show me where I am going wrong please.
(I know my recycling machines work if I manually add some reversed recipes in my mod's recipe.lua file, so it's not the machine that is broken!)
- ArderBlackard
- Long Handed Inserter
- Posts: 74
- Joined: Thu May 05, 2016 12:41 pm
- Contact:
Re: Adding recipes in control.lua
A recipe (as any prototype) can be defined only during game loading (in 'data.lua', 'data-updates.lua', 'data-final-fixes.lua'). The last one is probably the best option as '-final-fixes' scripts are executed after all other data definition stages. Then you may just set your recipe 'enabled' field to 'true' in an 'on_research_finished' handler instead of creating it on-the-fly.
Gib dich hin bis du Glück bist
Re: Adding recipes in control.lua
Thanks, I was heading to that conclusion.
At least most of my control.lua code can be moved to data-final-fixes.lua
At least most of my control.lua code can be moved to data-final-fixes.lua
Re: Adding recipes in control.lua
From inspection of the factorio-current.log it appears to load the the 'data-final-fixes.lua' in alphabetical order of the mod name.ArderBlackard wrote:A recipe (as any prototype) can be defined only during game loading (in 'data.lua', 'data-updates.lua', 'data-final-fixes.lua'). The last one is probably the best option as '-final-fixes' scripts are executed after all other data definition stages. Then you may just set your recipe 'enabled' field to 'true' in an 'on_research_finished' handler instead of creating it on-the-fly.
Code: Select all
0.180 Loading mod core 0.0.0 (data.lua)
0.181 Loading mod base 0.12.33 (data.lua)
0.225 Loading mod Crafted Artifacts 1.3.1 (data.lua)
0.253 Loading mod EvoGUI 0.4.15 (data.lua)
0.282 Loading mod test-mode 0.12.14 (data.lua)
0.309 Loading mod ZRecycling 0.0.1 (data.lua)
0.338 Loading mod test-mode 0.12.14 (data-updates.lua)
0.370 Loading mod test-mode 0.12.14 (data-final-fixes.lua)
0.397 Loading mod ZRecycling 0.0.1 (data-final-fixes.lua)
Re: Adding recipes in control.lua
If you really want to be after some mods final fixes add it as optional dependancy in json file(doesn't even need version).DRY411S wrote:From inspection of the factorio-current.log it appears to load the the 'data-final-fixes.lua' in alphabetical order of the mod name.ArderBlackard wrote:A recipe (as any prototype) can be defined only during game loading (in 'data.lua', 'data-updates.lua', 'data-final-fixes.lua'). The last one is probably the best option as '-final-fixes' scripts are executed after all other data definition stages. Then you may just set your recipe 'enabled' field to 'true' in an 'on_research_finished' handler instead of creating it on-the-fly.
Previously, when my mod was called just 'Recycling', it was loading before 'test-mode'Code: Select all
0.180 Loading mod core 0.0.0 (data.lua) 0.181 Loading mod base 0.12.33 (data.lua) 0.225 Loading mod Crafted Artifacts 1.3.1 (data.lua) 0.253 Loading mod EvoGUI 0.4.15 (data.lua) 0.282 Loading mod test-mode 0.12.14 (data.lua) 0.309 Loading mod ZRecycling 0.0.1 (data.lua) 0.338 Loading mod test-mode 0.12.14 (data-updates.lua) 0.370 Loading mod test-mode 0.12.14 (data-final-fixes.lua) 0.397 Loading mod ZRecycling 0.0.1 (data-final-fixes.lua)
Re: Adding recipes in control.lua
That would be a heck of a list To be *perfect*, my mod would need to be dependent on EVERY other mod that might add new recipes in 'data-final-fixes.lua'.orzelek wrote:If you really want to be after some mods final fixes add it as optional dependancy in json file(doesn't even need version).
Re: Adding recipes in control.lua
I would hope it's not that big of a problem since adding recipes in final fixes should be quite rare.DRY411S wrote:That would be a heck of a list To be *perfect*, my mod would need to be dependent on EVERY other mod that might add new recipes in 'data-final-fixes.lua'.orzelek wrote:If you really want to be after some mods final fixes add it as optional dependancy in json file(doesn't even need version).
Re: Adding recipes in control.lua
Yes agreed. All the recycling mods are going to (probably) need to do it, so there may be a clash between them. I know for example that the reverse-factory mod does this as I've looked at the code in that for some ideas, and will be acknowledging the author if (and when) I publish my mod.orzelek wrote:I would hope it's not that big of a problem since adding recipes in final fixes should be quite rare.
EDIT: Ack, it could be a problem if there is more than one mod building reverse recipes. The one that runs last, could reverse the reverse recipes that the others have built!
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Adding recipes in control.lua
I personally do not create any recipes in final-fixes stage. I tend to add a recipe in the Data phase, then edit them depending on what items exists in the data-updates phase, leaving final fixes... for final fixes, and other mods to edit things.
Re: Adding recipes in control.lua
So I am using data-final-fixes.lua now. The code seems to run fine creating the reverse recipes and adding them to data.
But the game is crashing with an error. Here's a truncated factorio-current.log
This is my first mod. Am I missing something fundamental here? It seemed to work when I was manually adding reverse recipes in a mod recipe file that was loaded.
But the game is crashing with an error. Here's a truncated factorio-current.log
Dump from log
If I do an error dump of that recipe in data-final-fixes.lua the recipe is serpent.block of the bad recipe
And the original recipe that I reversed is dumped asserpent.block of the originalrecipe
I don't seem to have corrupted that, compared with the declaration in the base factorio files.The recipe from the factorio file
The item-group and sub-group for "Recycling" and "recycling-machine", and the recipe category "recycling" are all defined. I know the iron-plate has dropped from 40 to 8, but that's part of the mod, to give only 20% of the original ingredients back.This is my first mod. Am I missing something fundamental here? It seemed to work when I was manually adding reverse recipes in a mod recipe file that was loaded.
Re: Adding recipes in control.lua
if you use a results table instead of a simple result string you need to provide type/name/amount keys in the sub-tables.
edit: seems like type = "item" is implicit. You still need the name and either amount or amount_min/_max.
edit: seems like type = "item" is implicit. You still need the name and either amount or amount_min/_max.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: Adding recipes in control.lua
That's got me going forward again thanks. After being explicit with type="item" and then removing it, but including 'name =' and 'amount =', I still get the same error. So on a whim I tried result = and result_count =. It works.prg wrote:if you use a results table instead of a simple result string you need to provide type/name/amount keys in the sub-tables.
edit: seems like type = "item" is implicit. You still need the name and either amount or amount_min/_max.
So it seems that if there is only one result from the recipe then I must use result = and result_count =
Re: Adding recipes in control.lua
results with just a single entry should work, too. But it needs to be a table in a table containing name and amount(_min/_max). Just specifying type alone doesn't help, it defaults to "item" anyway.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: Adding recipes in control.lua
It didn't work as a table, whatever combination I tried. If you look across the whole base files the only time 'results =' is used in recipe 'results= {table}' is when there is more than one result, or when min, max and probability are used. i.e. in fluid recipes.prg wrote:results with just a single entry should work, too. But it needs to be a table in a table containing name and amount(_min/_max). Just specifying type alone doesn't help, it defaults to "item" anyway.
Re: Adding recipes in control.lua
Code: Select all
data.raw.recipe["electronic-circuit"].result = nil
data.raw.recipe["electronic-circuit"].results = { {name="iron-plate", amount=5} }
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: Adding recipes in control.lua
Interesting that explicitly nilling the 'result = ' which is already present in the raw data and replacing it with 'results = {table}' works. Using 'results =' still gives me the error if I construct a local recipe and then data:extend(local_recipe)prg wrote:I can now turn one iron plate and three copper cables into five iron plates.Code: Select all
data.raw.recipe["electronic-circuit"].result = nil data.raw.recipe["electronic-circuit"].results = { {name="iron-plate", amount=5} }
EDIT: Found it. the 'results= table' had an extra redundant table within a table thus (using your example)
Code: Select all
results = { { {name="iron-plate", amount=5} } }
Last edited by DRY411S on Tue May 31, 2016 10:14 pm, edited 1 time in total.
Re: Adding recipes in control.lua
Could you provide the code?
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Re: Adding recipes in control.lua
See above I edited my post. Thanks for your help.prg wrote:Could you provide the code?
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Adding recipes in control.lua
I think with using a results table, you also need to specify a few extra tags... icon, order and subgroup, not to mention a locale entry for the recipe. some of these tags can be mitigated though if you use... I forget the tag... main_product? if you use that, then the 4 tags I mentioned earlier will be copied from the item listed on that tag.
Re: Adding recipes in control.lua
Ooh, please remember the tag, discussing how to address the locale issues for recipes created on the fly like this was going to be my next topic.bobingabout wrote:I think with using a results table, you also need to specify a few extra tags... icon, order and subgroup, not to mention a locale entry for the recipe. some of these tags can be mitigated though if you use... I forget the tag... main_product? if you use that, then the 4 tags I mentioned earlier will be copied from the item listed on that tag.
The mod is now functioning, icons are in place, category too. The order is alphabetic by recipe name, and the subgroups don't exist yet.