Load sound settings

Godot Version

4.2.1.stable

Question

Hi! I would like to ask for how exactely do I code loading simple settings. This is the code for saving audio (it’s on a scene):

func _on_save_pressed():
	var file = FileAccess.open("user://settings.data", FileAccess.WRITE)
	var settings = {}
	settings["master_volume"] = AudioServer.get_bus_index("Master")
	file.store_var(settings)
	file.close()

and this is the code for loading (it’s on autoload):

func _ready():
	if FileAccess.file_exists("user://settings.data"):
		var file = FileAccess.open("user://settings.data", FileAccess.READ)
		var settings = file.get_var()
		settings["master_volume"] = AudioServer.get_bus_index("Master")
		file.close()

The debugging tool say everything is ok. When I load it doesn’t load the settings, even though the file exists. I suspect it’s loading but not actually setting anything. How should I proceeed? Thanks!

From the documentation for FileAccess it also doesn’t seem to need the file.close() at the end (presumably because it is ref-counted).

Can you check if the code is actually storing the data you expect?
The user:// is located at $HOME\AppData\Roaming\Godot\app_userdata\<your-project-name> or you can open it from Godot via ProjectOpen User Data Folder.

Not sure if this helps, but using the ConfigFile gives you a pretty intuitive API to access config-files specifically.

This line looks a bit strange. Perhaps you wanted to use AudioServer.get_bus_volume_db(AudioServer.get_bus_index("Master"))?

And in your load function, it should be different as well:

AudioServer.set_bus_volume_db(AudioServer.get_bus_index("Master"), settings["master_volume"])

1 Like