Save the game's score

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

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.

:bust_in_silhouette: Reply From: Jowan-Spooner

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.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 =
	# 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, 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!

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

Nick888 | 2019-05-27 09:02

No, why do you ask?

Jowan-Spooner | 2019-05-27 09:14

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!

Nick888 | 2019-05-27 09:25