Godot guide · · 7 min read
Godot save game system: JSON, versions, migration, atomic write
A Godot 4.5 save system in GDScript: JSON with a version number, step by step migration, a checksum and a crash safe write, tested with real crashes.

Which save format should a Godot game use?
Use JSON for the game state, ConfigFile for settings, and do not save game state as .tres resources unless you trust every file you will ever load. JSON is readable, diffable, and the only one of these that cannot carry code. It costs you some type conversion.
The same 100 items, each with an id, a name, a count and a position, took 5,210 bytes as compact JSON, 9,219 indented, 6,197 as a ConfigFile, 10,460 with store_var, 14,392 as a binary resource and 18,079 as a text resource. The saving games tutorial says binary serialization should produce smaller files than JSON. With many small dictionaries it did not: every key is stored as a typed string. Measure your own data before choosing for size.
The trade-off is types. JSON has no Vector2 and every number comes back as a float. In the test, 120 parsed as a float, and a Vector2 written with JSON.stringify came back as the string "(1.0, 2.0)". Write positions as [x, y] arrays and wrap int() around counts you read back. ConfigFile, store_var and resources keep the types.

Where does user:// point on each platform?
Write saves to user://, never to res://, which is read only in an exported game. As of Godot 4.5 the data paths page gives these locations, with [project_name] taken from the project name:
- Windows:
%APPDATA%\Godot\app_userdata\[project_name] - macOS:
~/Library/Application Support/Godot/app_userdata/[project_name] - Linux:
~/.local/share/godot/app_userdata/[project_name] - Android and iOS: a folder unique to the project that other apps cannot read. The docs give no path.
- Web: a virtual filesystem stored in IndexedDB. I did not test saves in a browser for this guide, so check rename and flush behavior there before relying on them.
- Turn on
use_custom_user_dirin Project Settings before release if you do not want "Godot" in the folder path players see. The folder moves, so do it before the first public save exists.
How do you version a save and migrate old files?
Write a version number into every save and keep one function per step that turns version N into N + 1. On load, run the steps in order until the file reaches the current version. A player who skipped two updates then goes through two small steps, and you never write code that jumps from 1 straight to 3.
The example game changed twice. Version 2 renamed lvl to level and moved the music volume into settings. Version 3 added a list of cleared levels and a play time counter. A real version 1 file, loaded by the version 3 code, came out as coins 120, level 3, music_volume 0.8, cleared [1, 2, 3], play_seconds 0, as in the picture.
Two rules keep this working. Never edit an old step after release, because players still have files that depend on it. And refuse files with a version higher than your code: a player who moves back to an older build should see a message, not lose a save to a new game that overwrites it. The test returns the status too_new for that case.
# Each step turns the data of version N into the data of version N + 1.
static func _migrate(data: Dictionary, version: int) -> Dictionary:
match version:
1:
# "lvl" became "level", and audio moved into "settings".
return {
"coins": data.get("coins", 0),
"level": data.get("lvl", 1),
"settings": {"music_volume": data.get("music", 1.0)},
}
2:
# Cleared levels are stored now, and play time starts counting.
data["cleared"] = range(1, int(data["level"]) + 1)
data["play_seconds"] = 0
return data
return data
How do you write a save so a crash cannot destroy it?
Write the new save to a temporary file, close it, and only then rename it over the real one. DirAccess.rename overwrites an existing destination if it is not access protected, as the DirAccess reference says. A power cut during the write then leaves a broken .tmp next to an intact save.
Opening save.json for writing and storing the text straight into it is the mistake most people make. It truncates the file first. In the test, a write interrupted halfway left a file that failed its check, with no backup, so the player lost everything.
The code also moves the old save to save.json.bak before the swap, so the last good save always exists. flush() pushes the buffer to disk before the rename, and closing the file does the same. The FileAccess reference adds that stopping a running project with F8 kills the process without closing files, so testing a crash in the editor is easy.
# save_game.gd: versioned, checked JSON saves with an atomic write. Godot 4.5.
class_name SaveGame
extends RefCounted
const VERSION := 3
# Write to a temp file first, then swap it in. A crash leaves either the old save or the new one.
static func write(data: Dictionary, path: String) -> Error:
var payload := JSON.stringify(data, "\t", true)
var header := JSON.stringify({"version": VERSION, "sha256": payload.sha256_text()})
var tmp := path + ".tmp"
var file := FileAccess.open(tmp, FileAccess.WRITE)
if file == null:
return FileAccess.get_open_error()
file.store_string(header + "\n" + payload)
file.flush()
file.close()
if FileAccess.file_exists(path):
DirAccess.rename_absolute(path, path + ".bak")
return DirAccess.rename_absolute(tmp, path)
How do you load a save and recover from a bad one?
The first line of the file is a small header with the version and the SHA-256 of the rest. On load, hash the payload, compare, and only then parse it. A truncated file, a flipped byte and a hand edit all fail the same check, and the code tries save.json.tmp and save.json.bak before giving up. The hash is computed on the exact text that was read, not on parsed data, because parsing turns 5 into 5.0 and the hashes would never match.
Each case from the test, with an old save of 250 coins and a new one of 400:
# Returns {"status": "ok" | "missing" | "corrupt" | "too_new", "data": Dictionary, "source": String}.
static func load_data(path: String) -> Dictionary:
var first := _try(path)
if first.status == "ok" or first.status == "too_new":
return first
for backup in [path + ".tmp", path + ".bak"]:
var found := _try(backup)
if found.status == "ok":
return found
return first
static func _try(path: String) -> Dictionary:
if not FileAccess.file_exists(path):
return {"status": "missing", "source": path}
var text := FileAccess.get_file_as_string(path)
var split := text.find("\n")
var header: Variant = JSON.parse_string(text.substr(0, split)) if split > 0 else null
var payload := text.substr(split + 1)
if not header is Dictionary or header.get("sha256", "") != payload.sha256_text():
return {"status": "corrupt", "source": path}
var version := int(header.get("version", 0))
if version > VERSION:
return {"status": "too_new", "version": version, "source": path}
var data: Variant = JSON.parse_string(payload)
if not data is Dictionary:
return {"status": "corrupt", "source": path}
while version < VERSION:
data = _migrate(data, version)
version += 1
return {"status": "ok", "data": data, "source": path}- Crash while writing the temp file: loaded
save.json, 250 coins. The new save is lost, the old one is fine. - Crash between the two renames:
save.jsondid not exist, so it loaded the completesave.json.tmp, 400 coins. - One digit changed in
save.json: the check failed and it loadedsave.json.bak, 250 coins. - A file with version 99: status
too_new, no data, nothing overwritten. - Write finished:
save.json, 400 coins, andsave.json.bakholds the previous save.
Is it safe to save Resources as .tres files?
Not if the file can come from anywhere. A text resource can contain a script, and loading it runs that script. In the test, a 7 line .tres with an embedded GDScript set a flag in its _init the moment ResourceLoader.load returned it. Nothing was imported or attached. The same idea in a JSON file is just a dictionary.
Players share save files on forums and in modding communities, and a save that runs code is a way to hurt them. If you still want resources for your own data, load only files the game wrote itself, and treat anything imported as untrusted. The community project Godot Safe Resource Loader exists for this case, but its own README calls its checks a partial safeguard.
FileAccess.open_encrypted_with_pass encrypts a file with a password, which is useful against casual editing. The password ships inside the game, so it is not protection against a determined player.
When should the game save, and what else goes wrong?
Saving on a schedule is simple; saving at the right moments is what players notice:
- Save on events (level end, shop purchase, checkpoint), not every frame. A write of a few kilobytes is cheap, but it adds up on slow phone storage.
- On mobile, save when the app is paused. Nodes receive
NOTIFICATION_APPLICATION_PAUSEDin_notificationon Android and iOS, and the docs say iOS gives you about 5 seconds before the app is killed. - On desktop, handle
NOTIFICATION_WM_CLOSE_REQUESTfor a save on quit, and test with the real window, not the editor stop button. - Keep migrations in a test: store one real file per old version in your repo and load them all on every build, the way the state machine guide replays a recorded session.
- Do not crash on a missing key:
data.get("key", default)costs nothing, and a game economy number you add later needs a default for old saves. - Settings and progress change at different speeds. Put settings in their own file so a bad migration cannot reset the volume of a player who is 40 hours in.
- If saves feed a shop or score screen, send the same dictionary through signals instead of reading the file from five places.
Questions people ask
- Where does Godot save files on Windows, macOS and Linux?
- Under
user://, which maps to%APPDATA%\Godot\app_userdata\[project_name]on Windows,~/Library/Application Support/Godot/app_userdata/[project_name]on macOS and~/.local/share/godot/app_userdata/[project_name]on Linux, unless you set a custom user directory. - Why does JSON.parse_string return floats instead of integers?
- JSON has one number type, and Godot parses every number as a float. Cast with
int()when you read counts and ids, and store aVector2as an[x, y]array. - Is ConfigFile good for save games?
- It suits settings and small progress data, and it keeps types such as Vector2. For a large nested state with lists of items, JSON or a dictionary in
store_varis easier to migrate. - Does DirAccess.rename overwrite an existing file?
- Yes, if the destination is not access protected. That is what makes the temp file and rename pattern replace the old save in one step.