How to store (change) a value in JSON and call it in GDScript?

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

How can I store value and recall it after restarting Level? Say, I want to store Best Lap value and recall it when Level is restarted. How would you approach that? I’m new to storing/recalling methods. I’d highly appreciate one variable simple example of the complete procedure.

All I need is to store/replace the value of a variable (eg. BestLap) when a certain condition is met so that the value is preserved and called back after the restart. For instance, if the Best Lap was “3452”, I want it to show “3452” even after the Level restarted.

:bust_in_silhouette: Reply From: p7f

A simple save/load json example:

var save_game = File.new()
save_game.open("user://savegame.save", File.WRITE)
var current_scene_dict = {
	"best lap" : best_last_number
}
save_game.store_line(to_json(current_scene_dict))
save_game.close()

Then, to load:

var save_game = File.new()
if not save_game.file_exists("user://savegame.save"):
	return
save_game.open("user://savegame.save", File.READ)
var data = parse_json(save_game.get_line())
best_lap_number = data["best lap"]

Highly appreciated, mate! It’s because of kind people like you the beginners like me don’t quit.

Suleymanov | 2020-07-13 12:55

Im glad to help!

p7f | 2020-07-13 12:58