Way to write mod settings, particularly for updating

sparr
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Way to write mod settings, particularly for updating

Post 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.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14341
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Way to write mod settings, particularly for updating

Post 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.
If you want to get ahold of me I'm almost always on Discord.
sparr
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Way to write mod settings, particularly for updating

Post 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?
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1196
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: Way to write mod settings, particularly for updating

Post 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?
Rseding91
Factorio Staff
Factorio Staff
Posts: 14341
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Way to write mod settings, particularly for updating

Post by Rseding91 »

I can do that: allow scripts to change settings through migration scripts.
If you want to get ahold of me I'm almost always on Discord.
sparr
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: Way to write mod settings, particularly for updating

Post 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.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Way to write mod settings, particularly for updating

Post 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
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2920
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Way to write mod settings, particularly for updating

Post 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.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14341
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Way to write mod settings, particularly for updating

Post 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.
If you want to get ahold of me I'm almost always on Discord.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Way to write mod settings, particularly for updating

Post 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?
Rseding91
Factorio Staff
Factorio Staff
Posts: 14341
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Way to write mod settings, particularly for updating

Post 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.
If you want to get ahold of me I'm almost always on Discord.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Way to write mod settings, particularly for updating

Post 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?
Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: Way to write mod settings, particularly for updating

Post 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.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Way to write mod settings, particularly for updating

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

Return to “Implemented mod requests”