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.
save_file.store_line(to_json(data))
# Close file.
save_file.close()
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
save_file.close()
# 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!