Page 1 of 2

Adding recipes in control.lua

Posted: Mon May 30, 2016 7:13 am
by DRY411S
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!)

Re: Adding recipes in control.lua

Posted: Mon May 30, 2016 8:50 am
by ArderBlackard
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.

Re: Adding recipes in control.lua

Posted: Mon May 30, 2016 10:04 am
by DRY411S
Thanks, I was heading to that conclusion.

At least most of my control.lua code can be moved to data-final-fixes.lua

Re: Adding recipes in control.lua

Posted: Mon May 30, 2016 2:21 pm
by DRY411S
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.
From inspection of the factorio-current.log it appears to load the the 'data-final-fixes.lua' in alphabetical order of the mod name.

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)
Previously, when my mod was called just 'Recycling', it was loading before 'test-mode'

Re: Adding recipes in control.lua

Posted: Mon May 30, 2016 4:22 pm
by orzelek
DRY411S wrote:
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.
From inspection of the factorio-current.log it appears to load the the 'data-final-fixes.lua' in alphabetical order of the mod name.

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)
Previously, when my mod was called just 'Recycling', it was loading before 'test-mode'
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

Posted: Mon May 30, 2016 9:24 pm
by DRY411S
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).
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'.

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 7:10 am
by orzelek
DRY411S wrote:
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).
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'.
I would hope it's not that big of a problem since adding recipes in final fixes should be quite rare.

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 8:54 am
by DRY411S
orzelek wrote:I would hope it's not that big of a problem since adding recipes in final fixes should be quite rare.
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.

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!

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 9:24 am
by bobingabout
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

Posted: Tue May 31, 2016 7:46 pm
by DRY411S
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
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 as
serpent.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

Posted: Tue May 31, 2016 8:04 pm
by prg
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.

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 8:29 pm
by DRY411S
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.
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. :)

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

Posted: Tue May 31, 2016 8:35 pm
by prg
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

Posted: Tue May 31, 2016 8:39 pm
by DRY411S
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.
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.

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 8:48 pm
by prg

Code: Select all

data.raw.recipe["electronic-circuit"].result = nil
data.raw.recipe["electronic-circuit"].results = { {name="iron-plate", amount=5} }
I can now turn one iron plate and three copper cables into five iron plates.

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 10:08 pm
by DRY411S
prg wrote:

Code: Select all

data.raw.recipe["electronic-circuit"].result = nil
data.raw.recipe["electronic-circuit"].results = { {name="iron-plate", amount=5} }
I can now turn one iron plate and three copper cables into five iron plates.
:D 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)

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} } }

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 10:11 pm
by prg
Could you provide the code?

Re: Adding recipes in control.lua

Posted: Tue May 31, 2016 10:15 pm
by DRY411S
prg wrote:Could you provide the code?
See above I edited my post. Thanks for your help.

Re: Adding recipes in control.lua

Posted: Wed Jun 01, 2016 8:18 am
by bobingabout
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

Posted: Wed Jun 01, 2016 10:00 am
by DRY411S
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.
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. :)

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.