|
TraGUS
Godot plugin for user settings
|
Tragus is a neat piercing, but now, its is also an easy to use plugin to handle user-settings save, load, runtime edit, and easy UI-bindings.
The plugin is based on :
UserSettingsServer: It handles all the config file boiler plate, and lets you focus on setting up new user settings naturally.
It provides methods to save the current state of the user's settings into his config file, reset to default settings, or abort changes.
UserSetting: A base abstraction for you to extend into new user settings !
It provides methods to edit the setting at runtime, and stay synchronized with its real value (typically for UI elements)
The plugin allows, auto-magically :
.ini files - brief.ini files - brieftragus_default_settings.ini file - briefBelow, I wrote a tour on the workflow of the plugin, as well as a quick guide on what you need to do to implement new user settings, and how to setup UI to edit them.
However, it's sometimes easier to learn by example ! So I added a quick one in Examples.
tragus_default_settings.ini..ini config file..ini config file.You can read more about the detailed features in the full documentation of the plugin.
The workflow using TraGUS is pretty simple.
On game startup, each UserSettings registers itself automatically, and tries to initialize with either the user's .ini config file, or the tragus_default_settings.ini if the user's don't specify it.
To save the user's configuration in such file, see Taking effects.
You can create that file and edit it freely unde res://tragus_default_settings.ini, to setup the default configuration you wish for.
Make sure the section names and setting_keys you write in it do correspond to the Section and Key field of your UserSetting implementations.
You can optionnally specify a last resort default fall back in each UserSetting implementation.
This would notably allow you to, instead of writing the wholetragus_default_settings.iniby hand, simply run your game once and let UserSettingsServer initialize the file, then only edit the values in it.
New settings options are represented by concrete implementations of UserSetting. It could be a resolution setting, frame rate setting, or really anything, related either to the engine, or to your specific game itself.
Implementing such is fairly simple, in just 4 small steps described in Adding new user setting. You can also take a look at this resolution setting example.
A UserSetting is a singleton that exposes a few ways to edit it and stay synchronized, for example, through some UI nodes.
When you edit it, it takes effect immediately, but in cache. The setting isn't yet saved to the user's config file. For any effect on the actual config file, see the next section, UserSettingServer
MyUserSetting.Changed signal is emitted when the value of the setting changes. The first argument is the node that requested this change, the second is the new value of the setting. This can be used to stay in-sync accross multiple edit sources of the same setting.MyUserSetting.TryUpdateValue/GdTryUpdateValue sends a request to change the setting. The first argument is the node requesting this change, the second is the requested value. It returns false if the request was rejected.MyUserSetting.Reset sends a request to set the setting to the corresponding entry in tragus_default_settings.ini, and returns false if the request was rejected.The UserSettingsServer handles all the boilerplate to bind the settings to .ini config files.
UserSettingsServer.Save writes the current state of all registered UserSetting to the user's config file.UserSettingsServer.Abort aborts the current state of all registered UserSetting, and sets them back to the user's config file.UserSettingsServer.Reset(section) and UseSettingsServer.ResetAll() are little helpers built on top of UserSetting.Reset to respectively reset all registered settings under a given section, or simply all registered settings, to the corresponding value in tragus_default_settings.ini.First, you must enable the plugin, of course, under Project > Project Settings > Global > Plugins.
Then, you must make UserSettingsServer an autoload.
For this, go under Project > Project Settings > Global, then in Autoload, select the directory icon and browse in your game file to select addons/TraGUS/UserSettingsServer.cs.
Any UserSetting you will implement (see Adding new user settings) must appear lower in the autoloads list than the UserSettingsServer, that is, the Server must be loaded before the settings.
To add new user settings, you must implement a class extending UserSetting, and set it as an autoload.
You have only 4, straightforward, things to implement.
Defines the section this setting will be registered under in the user's .ini config file, and the setting name.
This setting for example, will be seen as follows in the .ini file.
Holds the actual behavior of the setting, like, changing the size of the window, the engine's frame rate, the color of the UI etc.
Is not required, you can optionnaly specify it to make sure the setting is always initialized to a valid value, even if none of the user's nor default .ini config file contains a valid entry for it.
If you really trust your tragus_default_settings.ini, you don't need to implement it.
Besides, there's a couple of methods you can overload for advanced behaviors.
Is called right before the the setting is registered, allows for some further initialization behavior, like loading some resources.
Allow to specify some advanced de/serialization mechanisms, if you use more complex settings values, like Arrays.
Once you have implemented these 4, function, you can register the node as an auto-load, just like you did for the settings server.
Any UserSetting you will implement (see Adding new user settings) must appear lower in the autoloads list than the UserSettingsServer, that is, the Server must be loaded before the settings.
Now, to bind this to the UI, nothing is simpler !
First, you must define a callback to keep the UI synchronized if other sources can modify the same setting.
The sender is the person responsible for the setting's value change. If it's ourself, we don't need to update the UI since we actively did it !
Now, you need to connect this callback to the setting's Changed signal, typically in your node's _ready function, and maybe also start your ui synchronized !
Let's say you have implemented a UserSetting, and registered it as autoload under the name SuperNeatSetting.
Finally, for your UI to be able to modify the setting, you simply need to send a request whenever you want to, for example, on a button click, or any other callback, while stamping the request with your name (self)
Additionnaly, you might want to handle the case where the operation was not successfull.
And that's it ! You can have as many ways to edit the same setting, and they'll stay synchronized !
Additionnaly, you can also reset a setting to a default value you have set in tragus_default_settings.ini using SuperNeatSetting.Reset().