Page 1 of 1

Way to write mod settings, particularly for updating

Posted: Thu Apr 27, 2017 8:53 pm
by sparr
Currently mod settings are read only. I am about to change the way a number setting in my mod works by a factor of 10. I need a way so that when a game using my mod gets loaded that was previously using on older version, I can /=10 that setting for that save.

Re: Way to write mod settings, particularly for updating

Posted: Thu Apr 27, 2017 9:45 pm
by Rseding91
You can change the valid range for your settings in the prototype which will force the loaded values to conform.

As for changing values: the values can only be changed by the player because if mods could change them then what the player picks doesn't really mean much because it doesn't stick with it.

Re: Way to write mod settings, particularly for updating

Posted: Thu Apr 27, 2017 11:10 pm
by sparr
In this case, the valid range didn't change enough to enforce a new value. And even if it did, I needed the new value to be 10x the old value.

To be specific, the value in question is a threshold amount of fluids. You guys 10x'd all the fluid amounts, so I needed to 10x the thresholds.

I agree that letting mods change settings all the time isn't great, but maybe it should be valid in migrations or init event handlers or somewhere?

Re: Way to write mod settings, particularly for updating

Posted: Fri Apr 28, 2017 7:40 pm
by Mooncat
sparr wrote:In this case, the valid range didn't change enough to enforce a new value. And even if it did, I needed the new value to be 10x the old value.

To be specific, the value in question is a threshold amount of fluids. You guys 10x'd all the fluid amounts, so I needed to 10x the thresholds.

I agree that letting mods change settings all the time isn't great, but maybe it should be valid in migrations or init event handlers or somewhere?
I think you mean migration script for settings?

Re: Way to write mod settings, particularly for updating

Posted: Fri Apr 28, 2017 7:56 pm
by Rseding91
I can do that: allow scripts to change settings through migration scripts.

Re: Way to write mod settings, particularly for updating

Posted: Fri Apr 28, 2017 9:41 pm
by sparr
Excellent!

Now I just need to figure out how real migration scripts work. My mods all use the global.mod_version global.data_version paradigm currently.

Re: Way to write mod settings, particularly for updating

Posted: Fri Apr 28, 2017 10:55 pm
by Nexela
Migration scripts are

migration_0.0.0.json Runs once and marked as ran in the map so it will not run again.
for stuff like entity - to - entity migration, recipe to recipe etc
This runs before the map loads so you can replace any entity that doesn't exist because you removed it to something else.

migration_0.0.0.lua Runs once and marked as ran in the map so it will not run again.
Has access to .game but not global
Commonly used for enabling/disabling recipes depending on research.

http://lua-api.factorio.com/latest/Migrations.html

The on_configuration_changed event is what you use to migrate stuff in your global variable
i.e you replace they way your script works
global.old_var = nil
global.new_var = {}
for mycode do populate new_var end

Re: Way to write mod settings, particularly for updating

Posted: Sun Apr 30, 2017 9:28 am
by Optera
Rseding91 wrote:I can do that: allow scripts to change settings through migration scripts.
Thanks, it's really needed.
I spent a good chunk of today hunting down errors due to invalid mod settings after changing the setting type or removing some options from a dropdown.

Re: Way to write mod settings, particularly for updating

Posted: Sun Apr 30, 2017 4:46 pm
by Rseding91
Optera wrote:
Rseding91 wrote:I can do that: allow scripts to change settings through migration scripts.
Thanks, it's really needed.
I spent a good chunk of today hunting down errors due to invalid mod settings after changing the setting type or removing some options from a dropdown.
That's just an error with the settings. It should force the value within the allowed range + allowed values when they change but it's not right now.

Re: Way to write mod settings, particularly for updating

Posted: Sat Feb 09, 2019 7:23 pm
by quyxkh
Rseding91 wrote:
Thu Apr 27, 2017 9:45 pm
You can change the valid range for your settings in the prototype which will force the loaded values to conform.

As for changing values: the values can only be changed by the player because if mods could change them then what the player picks doesn't really mean much because it doesn't stick with it.
On the other hand, refusing write access prohibits hotkeys for toggling settings. I toggle recipe groups/subgroups with a mod setting and also a hotkey, because for e.g. heavy signal work confining the display to the signal group is best but ordinarily I just want the whole lot, so control-g toggles that. But without mod access to update the setting, the two can get out of step. You have a setting for minimap display and allow mods to change that by hotkey or howsomever, why should mods be prevented from offering hotkeys to change their own settings?

Re: Way to write mod settings, particularly for updating

Posted: Sat Feb 09, 2019 7:50 pm
by Rseding91
quyxkh wrote:
Sat Feb 09, 2019 7:23 pm
Rseding91 wrote:
Thu Apr 27, 2017 9:45 pm
You can change the valid range for your settings in the prototype which will force the loaded values to conform.

As for changing values: the values can only be changed by the player because if mods could change them then what the player picks doesn't really mean much because it doesn't stick with it.
On the other hand, refusing write access prohibits hotkeys for toggling settings. I toggle recipe groups/subgroups with a mod setting and also a hotkey, because for e.g. heavy signal work confining the display to the signal group is best but ordinarily I just want the whole lot, so control-g toggles that. But without mod access to update the setting, the two can get out of step. You have a setting for minimap display and allow mods to change that by hotkey or howsomever, why should mods be prevented from offering hotkeys to change their own settings?
A hotkey is its own type of setting.

Re: Way to write mod settings, particularly for updating

Posted: Sat Feb 09, 2019 8:31 pm
by quyxkh
Yes, and a mod cannot implement code to make the mod's own hotkeys update any of the mod's own settings. At all. They can't synchronize the state of their own settings with anything players are allowed to change by any other means, hotkey, gui, nothing.

Does this really seem right to you, that a keystroke and at least seven clicks in various places on the screen are required to change mod behavior by updating a per-player runtime setting, and if the player goes through all that everything stays in sync, but the mod cannot offer a hotkey that works as well?

Re: Way to write mod settings, particularly for updating

Posted: Mon Mar 11, 2019 2:24 pm
by Boodals
Was this implemented in a 0.17 version? I think I remember people talking about it in the mod-making channel of the discord, but I might be wrong, and I can’t check right now because I’m away from home, on mobile.

Re: Way to write mod settings, particularly for updating

Posted: Tue Mar 26, 2019 8:55 pm
by quyxkh
It's fixed, I missed it in the changelog (it's there) but I saw a comment about it as well and just today got around to tryng it, I don't know whether this method's best, replacing the setting with a whole table as you do with fluid box contents, but it works:

Code: Select all

$ git diff -w @~ control.lua
diff --git a/control.lua b/control.lua
index 8951317..fc54a12 100644
--- a/control.lua
+++ b/control.lua
@@ -48,11 +48,9 @@ script.on_event(defines.events.on_runtime_mod_setting_changed, function(ev)
 
 script.on_event('item-grouping-toggle-groups',function(ev)
     local pidx, tag = ev.player_index, 'item-grouping-use-groups'
-    player_settings[tag][pidx] = not player_settings[tag][pidx]
-    apply_player_settings(pidx)
+    settings.get_player_settings(pidx)[tag] = { value = not player_settings[tag][pidx] }
     end)
 script.on_event('item-grouping-toggle-subgroups',function(ev)
     local pidx, tag = ev.player_index, 'item-grouping-use-subgroups'
-    player_settings[tag][pidx] = not player_settings[tag][pidx]
-    apply_player_settings(pidx)
+    settings.get_player_settings(pidx)[tag] = { value = not player_settings[tag][pidx] }
     end)

and the settings update redrives on_runtime_mod_setting_changed so all the behavior update code's in one place now, much cleaner.