Creating a save system
In theory, all we need to do is open a file, write the data we want to save to it, and then, later on, read that same file whenever we need the data. As it turns out, in the Godot Engine, it is indeed easy to read and write files.
We’re first going to see how to write to an external file.
Writing data to the disk
Let’s get to it by creating a new script called save_manager.gd
under the autoloads
folder. Then, to save data, put this code in the script:
extends Node const SAVE_FILE_PATH: String = "user://save_data.json" var save_data: Dictionary = { "highscore": 0 } func write_save_data(): var json_string: String = JSON.stringify(save_data) var save_file: FileAccess = FileAccess.open(SAVE_FILE_PATH, FileAccess.WRITE) if save_file == null: print("Could not open save file.") ...