Page 1 of 1

[0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Wed Feb 28, 2018 12:50 am
by Impatient
Hi,

I am devloping a mod. One of the features is, that the recipe for "electronic-circuit", defined in the base mod, can be disabled

(data.lua)

Code: Select all

data.raw.recipe["electronic-circuit"].enabled = false
data.raw.recipe["electronic-circuit"].normal.enabled = false
data.raw.recipe['electronic-circuit'].expensive.enabled = false
via an option in the mods startup settings.

This works fine for new games. But the problem is, that the change is not applied to loaded savegames. I had a hard time figuring out that the engine reacts differently on changes in data.lua when starting a new game and when loading a saved game (see viewtopic.php?f=25&t=58253 ).

After learning that, the next question was, how I can make the engine also apply the changes to savegames when they are loaded. After searching the web, forum and api-doc, the candidate was script.on_configuration_changed(...) ( http://lua-api.factorio.com/latest/LuaB ... on_changed ). But nothing worked as I hoped. So I did a systematic and painfully long and detailed empirical study on what works and what does not.

The executive summary of the solutions that worked is:
force.reset_technology_effects()

(control.lua)

Code: Select all

script.on_configuration_changed(function(ConfigurationChangedData)
        if ConfigurationChangedData.mod_startup_settings_changed == true then
                for _,force in pairs(game.forces) do
                        force.reset_technology_effects()
                end
        end
end)
force.recipes['electronic-circuit'].enabled = false
Ok, sure, but I was on the search for something that automatically applies all changes made in data.lua .

(control.lua)

Code: Select all

script.on_configuration_changed(function(ConfigurationChangedData)
        if ConfigurationChangedData.mod_startup_settings_changed == true then
                for _,force in pairs(game.forces) do
                        force.recipes['electronic-circuit'].enabled = false
                end
        end
end)
The list of things which did not work or did not work in a useful way can be found here.
Attempts that did not work
Here is the studies raw data:
Headache inducing details
Question before i mark this thread as solved:
Is the use of force.reset_technology_effects() in on_configuration_changed() the intended and complete way to update loaded savegames? Or is this something that lead to other problems later on / in other situations?

Thanks!

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Thu Mar 01, 2018 1:34 am
by Impatient
Found a solution to my problem. OP updated.

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Thu Mar 01, 2018 2:57 pm
by Nexela
Impatient wrote:Is the use of force.reset_technology_effects() in on_configuration_changed() the intended and complete way to update loaded savegames?
migration.lua files, They are only ever run once and marked as ran in the save file.

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Thu Mar 01, 2018 6:03 pm
by Impatient
This needs to run every time something changes via the startup options of the mod. Also if the game version or the mod itself does not change. If i disable the electronic-circuit recipe, this needs to be applied to the savegame. When I save and enable it again, this needs to run. When I save an disable it again, this needs to run. Do you still recommend migration files? Can you show me a code example? Thanks!

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Thu Sep 20, 2018 10:30 pm
by ownlyme
10/10 worst formatting ever :D
reset_technology_effects works terribly, for example you will loose the extra inventory size from that research-player-attributes (or so) mod.
i'm having a similar problem where i edit technologies but the changes sometimes don't apply to the recipes in a savegame.
thats why im currently setting recipes to enabled on init and configuration changed.

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Fri Sep 21, 2018 9:56 am
by eradicator
First things first: Write normally. That mess is barely readable.
Impatient wrote: Wed Feb 28, 2018 12:50 am Ok, sure, but I was on the search for something that automatically applies all changes made in data.lua .
You're looking for a logically impossible solution. The data stage only defines the default values. These default values can be changed by any number of things (other mods, the scenario, the player using console commands, etcpp) for any number of reasons and there is no way to automatically detect which recipes are enabled because they were "researched" and which are enabled because other things happend. Technically there might even be recipes that are disabled despite having been reserached. So the correct way to do what you want is to apply enabled.false = true to every recipe and every force you care about manually. As you want to dynamically change things according to mod options you'll want to apply it in on_configuration_changed as migrations (as mentioned) only run once.

If en/disabling a few recipes is the only thing you're doing than you don't really need to bother about the data stage at all though, you simply apply the changes during runtime with something like:

Code: Select all

local function update_recipes()
  --dostuff
  end

script.on_init(update_recipes)
script.on_configuration_changed(update_recipes)

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Fri Sep 21, 2018 10:28 am
by darkfrei
We are need for all recipes, for all researched technologies check if all recipes are enebled, but before disable all recipes for all unresearched technologies.
https://lua-api.factorio.com/latest/LuaTechnology.html
https://lua-api.factorio.com/latest/Lua ... otype.html
https://lua-api.factorio.com/latest/Con ... l#Modifier

Pseudocode:

Code: Select all

For force in forces do
  For technology in force.technologies do
    If technology is not researched then
      For effect in technology_prototype.effects do
        If effect.type == "unlock-recipe" then
          Recipe.enabled = false
        End
      End
    End
  End
  For technology in force.technologies do
    If technology is researched then
      For effect in technology_prototype.effects do
        If effect.type == "unlock-recipe" then
          Recipe.enabled = true
        End
      End
    End
  End
End

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Fri Sep 21, 2018 1:16 pm
by ownlyme
darkfrei your code won't be compatible with z adventure which rewards the player with recipes sometimes

Re: [0.16.27]Update loaded savegames to en-/disabled recipes?

Posted: Fri Sep 21, 2018 1:34 pm
by eradicator
ownlyme wrote: Fri Sep 21, 2018 1:16 pm darkfrei your code won't be compatible with z adventure which rewards the player with recipes sometimes
Which is btw exactly what i said above. Darkfreis code looks like it does pretty much the same as reset_tech_effects().
eradicator wrote: Fri Sep 21, 2018 9:56 am These default values can be changed by any number of things (other mods, the scenario, the player using console commands, etcpp) for any number of reasons and there is no way to automatically detect which recipes are enabled because they were "researched" and which are enabled because other things happend.
If a technology is researched = true or false tells you nothing about the recipes it (supposedly) unlocks.