I also tried removing the first settings.save(SAVE_LOCATION), but the result did not change
Many problems with your code snippet. But let’s first fix the main bug.
Sicne you start with empty file, calling get_value() in _ready() will fill your variables with null.
Later you pass those nulls to set_value(), and passing null removes the entries (look at set_value() reference in the docs). And the file remains empty because there’s no keys-values in the config file object at the moment you call save().
Call get_value() with default value passed as the third argument.
Can you explain a little more, please?
Read the ConfigFile class reference:
You can call get_value() with 3 arguments. In case the key is not found, that third argument will be returned. This is there precisely for cases like yours when you start with an empty config file.
resolution_index = settings.get_value("Graphic", "Resolution", 0)
current_display_mode = settings.get_value("Graphic", "Fullscreen", DisplayServer.WINDOW_MODE_FULLSCREEN)
Thank you. Well, the settings really started to be saved in settings.cfg (I opened the file myself and took a look), but they either reset at startup, or they don’t change at all
For start fix this:
music_volume_value = settings.get_value("Graphic","Resolution")
You’re assigning resolution to music volume.
And then let’s see how the code and the config file look now.
I’ve already noticed it, fixed it.
Code:
const SAVE_LOCATION = "user://settings.cfg"
var settings = ConfigFile.new()
var resolution_index = 0
var current_display_mode = DisplayServer.WINDOW_MODE_FULLSCREEN
var music_volume_value = -25
var main_theme_index = 0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var err := settings.load(SAVE_LOCATION)
resolution_index = settings.get_value("Graphic" , "Resolution" , 0)
current_display_mode = settings.get_value("Graphic" , "Fullscreen" , DisplayServer.WINDOW_MODE_FULLSCREEN)
music_volume_value = settings.get_value("Sound" , "MusicVolume" , -25)
push_warning("Loading settings error code: ", error_string(err))
get_node("StartButton").set_visible(1)
get_node("MainMenuGroup").set_visible(0)
pass # Replace with function body.
func _on_back_fs_pressed() -> void:
settings.set_value("Graphic", "Fullscreen" , current_display_mode)
settings.set_value("Graphic", "Resolution" , resolution_index)
settings.set_value("Sound", "MusicVolume" , music_volume_value)
var err := settings.save(SAVE_LOCATION)
push_warning("Loading settings error code: ", error_string(err))
print_debug(err)
get_node("SettingsGroup").set_visible(0)
get_node("MainMenuGroup").set_visible(1)
pass # Replace with function body.
This looks as expected. Is there still a problem somewhere?
Well, the settings, as I understand it, are written to a file. But it’s like they don’t change, or they don’t load.
Simply put, I exported this project, launched it, and saw this picture - changing the settings, and exiting the game - logging in again, everything returned to its place, that is, everything is standard
I may not explain well, because I have to use a translator, I’m sorry. ![]()
Do you even alter those variables you’re saving? We didn’t see that code.
Debugging 101. Print everything, every step of the way. Print values immediately after you get them from the config file and print them immediately before you set them to the config file. Print everything, trace everything, check everything. That’s how you see what your program is doing.
I can send the code in full, maybe it will help in some way.
No need. Learn to debug yourself. Put those prints in and see what’s happening with the values of your variables. Are they as you expecting them to be or not.
I used push_warning to output the values of the variables before get_value() and set_value() and after that. They don’t change. Is that how it should be?
First, don’t use push_warning() if there is nothing to warn about. Use print() or print_debug().
If the values in the settings.cfg are the same as printed values, then everything works as it should. The next step is to actually change one of those variables and check if the change is reflected in the config file.
Apparently, the functions responsible for the settings themselves do not return values when the settings themselves are changed.
Thanks, I’ll be digging in that direction.
What do you mean by that?
I assume that the parameter changes are simply not recorded in the variables, the values of which I write to config.
