–1 vote

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.

in Engine by (335 points)

1 Answer

+1 vote
Best answer

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"]
by (3,505 points)
selected by

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

Im glad to help!

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.