Audio Settings Are Writing To Save, but It Won't Read The Saved Values on Launch

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

The subsequent script is my SaveSystem, and it’s an AutoLoad scene that does what the name implies. When it’s booted up, it’s supposed to read the values from a cfg and set the variables it’s in charge of based on those values.

It does this correctly for Saving/Loading the last room the player was in, but it won’t read/load the values for the volume settings.

This line:

print("Last Room:", get_node("/root/SaveSystem").Last_Room, " Last Volume:", get_node("/root/SaveSystem").Old_Volume, " Last Music:", get_node("/root/SaveSystem").Old_Music)

-runs when the title screen launches, and it prints:
enter image description here

-so the Room save/load functionality is confirmed, but that same code doesn’t seem to be reading the values for Volume and Music correctly. The options menu is set to write those values into the file when you hit [Return] at the bottom of the menu, and it does; I can confirm it, but when the game starts up and SaveSystem runs it apparently doesn’t find anything there.

The Code in SaveSystem:

var Last_Room
var Old_Volume
var Old_Music

var save_path = "res://save-file.cfg"
var config = ConfigFile.new()
var load_response = config.load(save_path)

func _ready():
	#LOAD THE LAST CHECKPOINT FROM SAVE FILE
	Last_Room = get_node("/root/SaveSystem").Load_Room("Rooms", "LastRoom")
	Old_Volume = get_node("/root/SaveSystem").Load_Volume("Sound A", "Old_Volume")
	Old_Music = get_node("/root/SaveSystem").Load_Music("Sound B", "Old_Music")

#FUNCTION FOR SAVING THE LAST ROOM THE PLAYER WAS AT 
func Save_Room(section, key):
	config.set_value(section, key, Last_Room)
	config.save(save_path)
func Load_Room(section, key):
	Last_Room = config.get_value(section, key, Last_Room)

#FUNCTIONS FOR SAVING THE MASTER VOLUME SETTING
func Save_Volume(section, key):
	config.set_value(section, key, Old_Volume)
	config.save(save_path)
func Load_Volume(section, key):
	Old_Volume = config.get_value(section, key, Old_Volume)

#FUNCTINOS FOR SAVING THE MUSIC VOLUME SETTING 
func Save_Music(section, key):
	config.set_value(section, key, Old_Music)
	config.save(save_path)
func Load_Music(section, key):
	Old_Music = config.get_value(section, key, Old_Music)
:bust_in_silhouette: Reply From: Bernard Cloutier
func _ready():
    #LOAD THE LAST CHECKPOINT FROM SAVE FILE
    Last_Room = get_node("/root/SaveSystem").Load_Room("Rooms", "LastRoom")
    Old_Volume = get_node("/root/SaveSystem").Load_Volume("Sound A", "Old_Volume")
    Old_Music = get_node("/root/SaveSystem").Load_Music("Sound B", "Old_Music")

You are setting your variables to the return values of Load_Room, Load_Volume and Load_Music. So let’s see what those return values are!

func Load_Room(section, key):
    Last_Room = config.get_value(section, key, Last_Room)

func Load_Volume(section, key):
    Old_Volume = config.get_value(section, key, Old_Volume)

func Load_Volume(section, key):
    Old_Volume = config.get_value(section, key, Old_Volume)

Whoops! None of those have a return value. You set the correct variable to the correct value, but that value is then overwritten by null since you assign them the function’s non existant return value.

Don’t worry, mistakes like this happen to all of us :slight_smile:

BTW, why are you using get_node to get the current node? You could just remove every get_node("/root/SaveSystem"). call and just call the Load_... methods directly, since get_node("/root/SaveSystem") in this case is equivalent to self.

The extra get_node() is likely just leftovers; this code’s been copy+pasted a bit, and those lines probably came from a different node in the project originally.

I hear what your’e saying about the return values, but the one for loading rooms seems to work as intended. For saving, the .cfg file has a block for each and saves them consistently; writing something to the effect of:
“[Values] LastRoom=“C” [Sound_A] Old_Volume=-22.0 [Sound_B] Old_Music=-7.0”
and the SaveSystem.gd/tscn will load in the value for the room as intended with,

 func _ready():
 Last_Room = get_node("/root/SaveSystem").Load_Room("Rooms", "LastRoom")
 
 func Load_Room(section, key):
        Last_Room = config.get_value(section, key, Last_Room)

-but gets Null for the other two. Which, the load functions all look pretty identical to me, so I’m not really sure why one seems to be reading form the .cfg correctly, but the other one fails to: enter image description here

Strussle | 2020-02-21 20:50

Since I do not have the full code, all I can do is guess: I have a feeling that you are setting up the Last_Room somewhere else before your print statement.

I can assure you that at the end of the _ready function, the Last_Room variable is set to null. The only other option is that you have another line of code somewhere that sets the SaveSystem’s Last_Room variable to “C” before hitting the print statement.

Bernard Cloutier | 2020-02-21 21:13