I was just wondering if anyone could explain how to make dynamic recipes in mods. I am developing a mod called compressor and i want to add support for other mods. But right now i make each item by hand, i know there are mods that dynamically add recipes from other mods, such as reverse factory and a few of the other recycling mods. I just would like to add a script like this in the mod so that i can easily add support for bob's and youki's mods.
edit
someone helped me and now all is good, except some of my stuff is missing because of how tech sorting works
is there a way to check an items subgroup and use it to assign it to a tech category
becuase i want a tech that catches all subgroups someone may have added, so the argument would be does not equal any list of subgroups.
dynamic recipes in mods
-
- Inserter
- Posts: 40
- Joined: Sun Feb 22, 2015 7:54 pm
- Contact:
dynamic recipes in mods
Last edited by theradman221 on Fri Feb 03, 2017 3:58 am, edited 4 times in total.
Re: dynamic recipes in mods
Define them using variables instead of strings.
Code: Select all
for _, recipe in pairs(data.raw.recipe) do
if (recipe.result) then
data:extend ({
{ type = "recipe", name = "compressed-"..recipe.name, category = "compression", enabled = "false", ingredients = {{recipe.result, data.raw.item[recipe.result].stack_size]}}, result = "compressed-"..recipe.result", result_count = 1 },
{ type = "recipe", name = "uncompressed-"..recipe.name, category = "decompression", enabled = "false", ingredients = {{"compressed-"..recipe.result, 1}}, result = recipe.result, subgroup = "uncompression", result_count = data.raw.items[recipe.result].stack_size] },
})
table.insert(data.raw.technology["Compression"].effects, {type="unlock-recipe", recipe="compressed-"..recipe.name})
table.insert(data.raw.technology["Compression"].effects, {type="unlock-recipe", recipe="uncompressed-"..recipe.name})
end
end
-
- Inserter
- Posts: 40
- Joined: Sun Feb 22, 2015 7:54 pm
- Contact:
Re: dynamic recipes in mods
ok, that goes in the control file right, Right now i don't even use a control file so i don't actually know how to use one....
also i put that in a control file i made and it keeps saying unexpected ] instead of } and after i messed around it then i found out that the string was unfinished
also i put that in a control file i made and it keeps saying unexpected ] instead of } and after i messed around it then i found out that the string was unfinished
Re: dynamic recipes in mods
You cannot have recipes altered during the game, so no control lua.
data.lua, data-updates.lua and data-final-fixes.lua are script files, you can write programs there.
Dave's code goes to data-updates.lua, and it adds compress\uncompress recipes for every recipe with a single result.
data.lua, data-updates.lua and data-final-fixes.lua are script files, you can write programs there.
Dave's code goes to data-updates.lua, and it adds compress\uncompress recipes for every recipe with a single result.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
I also update mods, some of them even work.
Recently I did a mod tutorial.
-
- Inserter
- Posts: 40
- Joined: Sun Feb 22, 2015 7:54 pm
- Contact:
Re: dynamic recipes in mods
ok thanks i am going to try that, i knew there was something wrong i just have never had to actually use anything other than the data.lua for the mod to work in the past
Edit, it still doesn't work
i tried about 50 different ways of tweaking the code slightly and then when it did finally decide to start the game without errors it crashed because it took around 20 mins of nothing before crashing.
Edit, it still doesn't work
i tried about 50 different ways of tweaking the code slightly and then when it did finally decide to start the game without errors it crashed because it took around 20 mins of nothing before crashing.
Re: dynamic recipes in mods
I guess inserting into data.raw.recipe while looping through it leads to an infinite loop. Try defining a new table and using that instead.
Code: Select all
local old_recipes = {}
for _, recipe in pairs(data.raw.recipe) do
table.insert(old_recipes, recipe)
end
Re: dynamic recipes in mods
Curious if anyone was able to find a solution to this, I'm trying to find a way myself of making item recipes adjust mid-game
Re: dynamic recipes in mods
um. there's now oodles of different mods that do this very thing that you could have a look at for an example of how to do this.
Probably one of the most comprehensive is omicompression, which compresses everything; there's the various automatic barrelling mods (which aren't updated, now that barrelling is part of the core game).
If you want to change a recipe while the game is playing; that's not actually possible. What you can do, though, is to prepare a number of different recipes ahead of time, and change them around at runtime. The mod I know of that does this the most smoothly is omnimatter; research unlocks more efficient recipes over time, and all of your crafting machines get upgraded to run the best recipe you have available.
Probably one of the most comprehensive is omicompression, which compresses everything; there's the various automatic barrelling mods (which aren't updated, now that barrelling is part of the core game).
If you want to change a recipe while the game is playing; that's not actually possible. What you can do, though, is to prepare a number of different recipes ahead of time, and change them around at runtime. The mod I know of that does this the most smoothly is omnimatter; research unlocks more efficient recipes over time, and all of your crafting machines get upgraded to run the best recipe you have available.
Re: dynamic recipes in mods
I have a few mods out that use dynamic recipes.
What I did was set up a variable within the module set up that is user configurable. Then based on the variable selected, the mod defines the recipe making it more or less expansive based on the set up values.
Once the mod is loaded, there is no easy or practical way to change that. But if the player wanted, they would save the game, start a new game, reset the set up values to something new, restart Factorio and reload the save. The new more, or less expansive recipes would based on the set up values would be in effect.
The best and more visible example of this is in https://mods.factorio.com/mod/Hiladdar_Electrical, particularly solar panels and accumulators, the way it works, is the more effective the solar panels and accumulators are, the more expensive the recipe is, and it gives the player the option to have that item scale based on player preference.
The following is extracted from the setting.lua
I found a lua function to convert a floating point number to an integer, and used that within the recipe file for solar panel. Testing revealed that I did not have to use the toint(n) function, but I just left it in there since it does not break anything and I can use in the future as an example of how to incorporate a function within lua:
Then within recipe definition, have the recipe dynamically set during the module's load time.
Hiladdar
What I did was set up a variable within the module set up that is user configurable. Then based on the variable selected, the mod defines the recipe making it more or less expansive based on the set up values.
Once the mod is loaded, there is no easy or practical way to change that. But if the player wanted, they would save the game, start a new game, reset the set up values to something new, restart Factorio and reload the save. The new more, or less expansive recipes would based on the set up values would be in effect.
The best and more visible example of this is in https://mods.factorio.com/mod/Hiladdar_Electrical, particularly solar panels and accumulators, the way it works, is the more effective the solar panels and accumulators are, the more expensive the recipe is, and it gives the player the option to have that item scale based on player preference.
The following is extracted from the setting.lua
Code: Select all
data:extend({
{
type = "double-setting",
name = "hsmd-solar",
setting_type = "startup",
minimum_value = 1.5,
maximum_value = 10,
default_value = 3,
order = "electrical-1"
}
})
Code: Select all
function toint(n)
local s = tostring(n)
local i, j = s:find('%.')
if i then
return tonumber(s:sub(1, i-1))
else
return n
end
end
Code: Select all
{
type = "recipe",
name = "hsmd-solar-panel-mk2",
enabled = false,
energy_required = 10,
ingredients =
{
{"solar-panel", (settings.startup["hsmd-solar"].value + .5) },
{"copper-cable", ((settings.startup["hsmd-solar"].value * (settings.startup["hsmd-solar"].value-1)) +.49999) },
{"advanced-circuit", ((settings.startup["hsmd-solar"].value ^ .5) * 10 + .49999) }
},
result = "hsmd-solar-panel-mk2"
},