0 votes

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()
        SFile.close()
        print(GameData)

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.

Godot version 3.4.4
in Engine by (25 points)

1 Answer

+1 vote
Best answer

Is variables another script?

Should it be

"Level" : $Variables.Level,
by (2,001 points)
selected by

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

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():
    add_savedata()
    var file = File.new()
    file.open("res://SaveScore.dat", File.WRITE)
    file.store_var(savedata)
    file.close()

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()
        file.close()
        unpack_load()


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"]

Thank you very much, It works now!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.