Page 1 of 1
Enable recipe if tech already researched
Posted: Sun Apr 17, 2016 5:00 pm
by JasonC
If I make a new item that's part of an existing tech, e.g. via data.lua:
Code: Select all
table.insert(data.raw["technology"]["advanced-electronics"].effects, { type = "unlock-recipe", recipe = "my-new-item"})
It seems the recipe doesn't become available unless I actually perform the action of researching that technology. If it's already researched when I add the mod, the recipe remains disabled.
What is the best way, I guess somewhere in control.lua, to enable the recipe if the tech is already researched, so that the item will be immediately available for people who add the new mod and load an existing save that already has the tech researched?
Re: Enable recipe if tech already researched
Posted: Sun Apr 17, 2016 5:05 pm
by prg
In on_init (when the mod first gets added) or on_configuration_changed (if you added an item to a later version of the mod):
Code: Select all
for _, force in pairs(game.forces) do
if force.technologies["some-technology"].researched then
force.recipes["my-recipe"].enabled = true
end
end
Re: Enable recipe if tech already researched
Posted: Sun Apr 17, 2016 5:12 pm
by JasonC
Sweet thanks.
Just now I also found, from
https://wiki.factorio.com/index.php?tit ... on_scripts, a way to do it in a migration script, and I'm looking at the source for another mod that also does it there.
Is there any reason to prefer control.lua's on_init vs. a migration script?
Re: Enable recipe if tech already researched
Posted: Sun Apr 17, 2016 5:42 pm
by prg
Sounds like a migration would only run when the mod version changes, not when it's initially added. So it seems like an alternative to on_configuration_changed. Haven't used a migration script myself yet but done everything in control.lua so far, so not sure.
Re: Enable recipe if tech already researched
Posted: Sun Apr 17, 2016 6:39 pm
by JasonC
prg wrote:Sounds like a migration would only run when the mod version changes, not when it's initially added. So it seems like an alternative to on_configuration_changed. Haven't used a migration script myself yet but done everything in control.lua so far, so not sure.
Hm I'll have to experiment. Since I only have one version, the migration script runs the first time it's loaded for a game. So it looks like it has the desired behavior. But I don't know now if it's reliable. Doing it like you say seems 100% reliable.
But I'll test to see if the migration scripts are the same effect (and update the wiki if not, because in that case it'd be kind of misleading to have it as an example there).
Re: Enable recipe if tech already researched
Posted: Sun Apr 17, 2016 8:52 pm
by Adil
prg wrote:Sounds like a migration would only run when the mod version changes, not when it's initially added. So it seems like an alternative to on_configuration_changed. Haven't used a migration script myself yet but done everything in control.lua so far, so not sure.
There's the case of {old_version = nil, new_version= something} discussed on the
wiki, I believe I even use that successfully in some mod. I also believe it is only risen when mod is added to running game, but for a new game on_init() should be used instead.
Re: Enable recipe if tech already researched
Posted: Mon Apr 18, 2016 5:59 am
by JasonC
Yeah after further experiments migrations is not the place to do it; on_configuration_changed (when going from nil to non-nil versions) is the ticket.