Skip to content

How do I save and load the game?

Godot 4.x

The script sits on Main and holds the values worth keeping:

  • MainNode2Dhas scriptthe script goes here

Gather those values into a Dictionary of plain types: numbers, strings, booleans and arrays of them. Save values, not nodes.

main.gd
extends Node2D
var coins := 0
var level := 1
func get_save_data() -> Dictionary:
return {
"coins": coins,
"level": level,
"position": [position.x, position.y],
}

Open a file for writing, turn the dictionary into a JSON string, and store it. The file closes on its own when the variable goes out of scope:

main.gd
func save_game() -> void:
var data := get_save_data()
var file := FileAccess.open("user://save.json", FileAccess.WRITE)
file.store_string(JSON.stringify(data))

user:// is a folder Godot owns for your game, writable on every platform; open it from the editor with Project → Open User Data Folder to see save.json appear. The extension is up to you: .json is a hint to people reading the folder, but .save, .dat or no extension at all works exactly the same, because Godot only cares about the text inside.

Check the file exists first, so the very first run doesn’t crash. Then open it for reading, parse the text, and copy the values back:

main.gd
func load_game() -> void:
if not FileAccess.file_exists("user://save.json"):
return
var file := FileAccess.open("user://save.json", FileAccess.READ)
var data = JSON.parse_string(file.get_as_text())
if data == null:
return
coins = int(data["coins"])
level = int(data["level"])
position = Vector2(data["position"][0], data["position"][1])

JSON.parse_string() returns null when the text isn’t valid JSON, which is why there is a second check. JSON has no integer type, so every number comes back as a float: data["coins"] is 12.0, not 12, and int() turns it back.