0 votes

Hi guys.

i want when the score is etc 20 to be saved automatically and then when the score is etc 30 to be saved automatically too. Is there a tutorial about this motif of saving?

Thank you.

in Engine by (223 points)

1 Answer

+1 vote
Best answer

Hey Nick88,
I think a combination of an if statement and a normal saving should do it:

var my_data = {"Score":0}
func update_score(new_score):
    my_data["Score"] = new_score
    if new_score%10 == 0:
        save_data(my_data, "save_file", "user")

If you want to save at special points (in the example above it's saved in steps of ten) you should use an Array (save_points = [20, 30, 45, 52]) and then use if new_score is in save_points.

Here are the save/load functions I usually use:

func save_data(data, file, path = "res"):

    ## Create and open your file.
    var SAVE_PATH = path+'://'+file+'.json'
    var save_file = File.new()
    save_file.open(SAVE_PATH, File.WRITE)

    # Convert your data to a useable string-format.

    # Close file.

func load_data(file, empty = null, path = "res"):

    # Create your file
    var SAVE_PATH = path+"://"+file+".json"
    var save_file = File.new()

    # Return 'empty' if file doesn't exist
    if not save_file.file_exists(SAVE_PATH):
        print("ERROR: file does not exist ("+path+"://"+file+".json)")
        return empty

    # Open your file
    save_file.open(SAVE_PATH, File.READ)

    # Save data from file.
    var data = parse_json(save_file.get_as_text())

    # Close file 

    # Return data
    return data

You can then load your data (for example when starting your project) with:

my_data = load_data("save_file", my_data, "user")

Hope this helps. Good Luck!

by (864 points)
selected by

Hmmm...Thank you so much!!!
By the way, do you have a channel on YouTube?

No, why do you ask?

I was just thinking that if you had a channel you would be creating great Godot tutorials and i would making a sign up of it for sure.You helped me a lot of times...
Thanks again!

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.