Could I please get the changes explained step-by-step?
Sure, though I'm just going to explain each chunk instead of step-by-step, as it will be faster and easier to write, and it shouldn't impact the explanation any:
extends VBoxContainer
const AUDIO_FILEPATH = "user://audio_settings.cfg"
var config_file
AUDIO_FILEPATH
is the filepath we are going to save the config file at. The user://
directory is a special Godot directory that maps to a location on the file system that can be read and written to by Godot. There's a page on the documentation if you are curious about where this file goes, which I can link to later if desired.
config_file
is just a class variable that we'll be using to hold the reference to the ConfigFile, nothing fancy.
func _ready():
config_file = ConfigFile.new()
var error = config_file.load(AUDIO_FILEPATH)
if error != OK:
print ("Audio configuration file not found!")
else:
print ("Audio configuration file loaded")
First, this function makes a new ConfigFile and assigns it to the config_file
variable. Then, we try to load the file at AUDIO_FILEPATH
, which is a location where the file will be stored. We assign the returned value of the load
function to a variable called error
. Then we check to see if error
is NOT equal to OK
, and this will occur if the file is not found, meaning no configuration has been saved yet. If error
is equal to OK
, then that means we have successfully loaded the config file.
func _on_Sound_value_changed(value):
AudioServer.set_bus_volume_db(
AudioServer.get_bus_index("Sounds"),
linear2db(value) )
# Save the setting and file
config_file.set_value("audio", "sound_volume", linear2db(value))
config_file.save(AUDIO_FILEPATH)
Nothing fancy here until the bit after the comment. What we are doing here is storing the new sound volume in the config file, in the section audio
with the key sound_volume
. This means we can later access this value using the same section and key combo. Finally, we tell the config file to save itself so when we need to load it later, we have up-to-date values.
func _on_Sounds_ready():
if (config_file.has_section_key("audio", "sound_volume"):
var volume = config_file.get_value("audio", "sound_volume")
$Sounds/Slider.value = volume
else:
$Sounds/Slider.value = AudioServer.get_bus_volume_db(
AudioServer.get_bus_index("Sounds") )
# New version based on Calinou's suggestion (remove version above if using new version)
# var volume = config_file.get_value("audio", "sound_volume", AudioServer.get_bus_volume_db(AudioServer.get_bus_index("Sounds")))
# $Sounds/Slider.value = volume
First, we check to see if there is a value stored in the section audio
with the key sound_volume
. If there is, then we get that value from config_file
using the get_value
function and assign it to a function variable called volume
. We then assign the value of the slider to this value.
If there is NOT a value stored in the section audio
with the key sound_volume
, then we simply get the volume from the AudioServer like normal.
However, as @Calinou mentioned, we can simplify this code by taking advantage of the get_value
function. The third, optional, argument in the get_value
function will be returned if the key does not exist in the config_file
. So, we can make this third argument the AudioServer's sound instead, allowing us to use just two lines of code. If the value exists in the config_file
, then the new version will return it, while if it does not, the new version will simply return the third argument instead.
func _on_Music_value_changed(value):
AudioServer.set_bus_volume_db(
AudioServer.get_bus_index("Music"),
linear2db(value) )
# Save the setting and file
config_file.set_value("audio", "music_volume", linear2db(value))
config_file.save(AUDIO_FILEPATH)
Nothing fancy here until the bit after the comment, and its the same as the _on_Sound_value_changed
function, just for music.
func _on_Music_ready():
if (config_file.has_section_key("audio", "music_volume"):
var volume = config_file.get_value("audio", "music_volume")
$Music/Slider.value = volume
else:
$Music/Slider.value = AudioServer.get_bus_volume_db(
AudioServer.get_bus_index("Music") )
pass
# New version based on Calinou's suggestion (remove version above if using new version)
# var volume = config_file.get_value("audio", "music_volume", AudioServer.get_bus_volume_db(AudioServer.get_bus_index("Music")))
# $Music/Slider.value = volume
Same thing here: It's the same code as _on_Sound_ready
, it's just for music instead of sound. As I mentioned in the note above, you can simplify the code.
Hopefully that helps explain things a bit!