How Make Menu Changes Stay?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Noddy

I’m making the settings menu for my game, I’ve already made the autoload script and the config file, when the default settings are used the the settings menu is fine, it displays the actual settings being used. If you use the settings menu and change things on it they will apply to the config file and will be used, but when you leave the settings menu and come back the settings menu will be wrong but the settings will stay the same. How do I make those changes stick around after I leave and when I reload the game? I need help with changing parameters on nodes such as check buttons and text inputs so I can have them set to what I want them to be from code, in fact just this knowledge would be enough as I could check the current user settings against the default settings and have the buttons and text fields be changed from the code. Though there might be a better way that could be shown to me.

:bust_in_silhouette: Reply From: Zylann

For such menu there are usually two functions doing the work, something like load_from_settings() and save_to_settings(). I guess you need the former.

Your menu might revert back to its default state if you destroyed and re-instanced it, or if you restarted the game. So when it is _ready(), or when it is shown (NOTIFICATION_VISIBILITY_CHANGED), you need to update all its controls according to the actual settings you loaded from the file.

This can be done case by case: for example, if you have a Slider for volume, you would set it like this, assuming settings is what you loaded from the config file:

get_node("VolumeSlider").ratio = settings.volume_linear

Or if you have a checkbox for particle effects:

get_node("ParticleEffects").pressed = settings.particle_effects_enabled

Alternatively, there are some settings that you can get directly from the engine. For example, volume can be retrieved from the main audio bus:

get_node("VolumeSlider").ratio = db2linear(AudioServer.get_bus_volume_db(0))

One twist you need to care about is that when you set controls values like this, it may trigger modification signals. If you have code connected to them, they would be called as if the user changed the settings. It should be fine most of the time, but it’s good to be aware of this.