Load variable from file to label

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

I made a save and load system and I’m sure that save works fine because I made two buttons, one changes variables other one save files. I used a hex editor and the value was equal to variable so it works. But problem is with loading. I have an button that calls LoadData() function (shown bellow)

func LoadData():
var SFile = File.new()
if SFile.file_exists(SavePath):
	var SError = SFile.open(SavePath, File.READ)
	if SError == OK:
		var GameData = SFile.get_var()

After pressing load value should show in a label. Bellow is a dictionary:

var GameData = {
	"Level" : Variables.Level,

(I use global variables)

Here is label code:

func _process(_delta):
	$LevelStarDisplay.text = str(Variables.Level)

And as I said after loading this “Level” variable should be shown in this label but it’s not. Default value is 0 and it’s showing 0 after running again and pressing load button even though hex editor shows right value. I also tried using “print()” to check if LoadData() function reads it right and it does because it printed out everything right. print(GameData)

If needed I can show more code.

Any help will be appreciated. Thanks.

:bust_in_silhouette: Reply From: Gluon

Is variables another script?

Should it be

"Level" : $Variables.Level,

Yes It is in another script but I put it in Autoload.

NeiPodam | 2022-06-17 20:55

Hi I am sorry I have been drinking so I really shouldnt have come on here to answer any questions. Below is some code I wrote for an old game which worked, hopefully it will help you.

var savedata = {}
func save_score():
	var file = File.new()
	file.open("res://SaveScore.dat", File.WRITE)
func add_savedata():
	savedata = {
		"difficulty" : difficulty,
		"highscore" : highscore,
		"language" : language,
		"move_left" : move_left,
		"move_right" : move_right,
		"move_up" : move_up,
		"move_down" : move_down,
		"main_fire" : main_fire,
		"alt_fire" : alt_fire
func load_score():
	var filecheck = File.new()
	if filecheck.file_exists("res://SaveScore.dat"):
		var file = File.new()
		file.open("res://SaveScore.dat", File.READ)
		savedata = file.get_var()
func unpack_load():
	difficulty = savedata["difficulty"]
	highscore = savedata["highscore"]
	language = savedata["language"]
	move_left = savedata["move_left"]
	move_right = savedata["move_right"]
	move_up = savedata["move_up"]
	move_down = savedata["move_down"]
	main_fire = savedata["main_fire"]
	alt_fire = savedata["alt_fire"]

Gluon | 2022-06-17 21:51

Thank you very much, It works now!

NeiPodam | 2022-06-18 08:11