Enable recipe if tech already researched

Place to get help with not working mods / modding interface.
Post Reply
JasonC
Filter Inserter
Filter Inserter
Posts: 448
Joined: Tue Mar 22, 2016 3:05 am
Contact:

Enable recipe if tech already researched

Post 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?
Took a break from 0.12.29 to 0.17.79, and then to ... oh god now it's 1.something. I never know what's happening.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Enable recipe if tech already researched

Post 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
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

JasonC
Filter Inserter
Filter Inserter
Posts: 448
Joined: Tue Mar 22, 2016 3:05 am
Contact:

Re: Enable recipe if tech already researched

Post 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?
Took a break from 0.12.29 to 0.17.79, and then to ... oh god now it's 1.something. I never know what's happening.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Enable recipe if tech already researched

Post 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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

JasonC
Filter Inserter
Filter Inserter
Posts: 448
Joined: Tue Mar 22, 2016 3:05 am
Contact:

Re: Enable recipe if tech already researched

Post 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).
Took a break from 0.12.29 to 0.17.79, and then to ... oh god now it's 1.something. I never know what's happening.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Enable recipe if tech already researched

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

JasonC
Filter Inserter
Filter Inserter
Posts: 448
Joined: Tue Mar 22, 2016 3:05 am
Contact:

Re: Enable recipe if tech already researched

Post 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.
Took a break from 0.12.29 to 0.17.79, and then to ... oh god now it's 1.something. I never know what's happening.

Post Reply

Return to “Modding help”