Why is the updated value of my global variable not being passed?

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

Hello, I am still a complete beginner in Godot and I have this problem. I have a global variable “volume” which will be the one to update the hslider value of my music settings. In my start game scene, I have created a file and a function which will save any change in the music settings.

const SAVE_MUSIC = "user://savemusicsettings.save"

func save_settings():
var save_data = File.new()
save_data.open(SAVE_MUSIC, File.WRITE)
save_data.store_var(Global.volume)
save_data.close()

So I have two music settings, one in the start game scene, and one in the main scene. My goal is that the value of any volume change that happened on the music settings of the start scene will be updated and passed to the music settings of the main scene when it starts. So I also created a load_settings function

func load_settings():
var save_data = File.new()
#need to check if save file exist
if save_data.file_exists(SAVE_MUSIC):
	save_data.open(SAVE_MUSIC, File.READ)
	Global.volume = save_data.get_var()
	save_data.close()

To check if the value of “volume” is updated, I print it in the ready function of the main scene and it is updated but it is not passed on the hslider value of the music settings in the main scene.

func _ready():
load_settings()
$ColorRect/HSlider.value = Global.volume

What could be the problem? Thank you.

:bust_in_silhouette: Reply From: Gluon

The problem is you are updating the indicator of the volume but not the actual volume on the bus itself. You will need some code on a screen which loads at the start of your game in the

_ready():

function which sets the volume based on the save value you have in your global file. On my games I create a splash screen as the first screen it loads too which just shows my name as the developer and an image I created which I use to load these sorts of variables.