← Pixel Dreamer Studio

A JSON save that loads back exactly what you saved

JSON only knows numbers, text, lists and objects with text keys. Let Godot write the types for you.

JSON has one kind of number and only text keys, so JSON.parse_string() gives every number back as a float and every dictionary key as a String. In our Godot 4.7 test 5 coins came back as 5.0 and the label said Coins: 5.0, a farm plot saved under Vector2i(2, 3) was no longer found, and an item saved under the ID 7 was only found as the text "7". Writing the file with JSON.from_native() and reading it with JSON.to_native() gave back the int 5, the Vector2i key and the int key.

The code

Save it as json_save.gd and use it in place of your own file code: const SaveJson := preload("res://json_save.gd"), then SaveJson.write("user://save.json", data) and var data = SaveJson.read("user://save.json", {}). read() gives back the fallback when the file is missing or broken, so a first launch or a save cut off halfway does not crash the game. from_native() and to_native() need Godot 4.4 or later.

extends RefCounted
## Saves and loads game data as JSON and gives back the same types you saved.
## JSON.stringify() + JSON.parse_string() turn every number into a float and
## every dictionary key into a String, so 5 coins load as 5.0, and farm plots
## keyed by Vector2i or items keyed by an int ID are no longer found.
## JSON.from_native() / JSON.to_native() (Godot 4.4 and later) store the type
## next to each value, so ints, Vector2i keys and Vector2 come back as they were.
##   SaveJson.write("user://save.json", data)
##   var data = SaveJson.read("user://save.json", {})   # {} if missing or broken


static func write(path: String, data) -> bool:
	var f := FileAccess.open(path, FileAccess.WRITE)
	if f == null:
		return false
	f.store_string(JSON.stringify(JSON.from_native(data), "\t"))
	return true


static func read(path: String, fallback = null):
	if not FileAccess.file_exists(path):
		return fallback
	var json := JSON.new()
	if json.parse(FileAccess.get_file_as_string(path)) != OK:
		return fallback  # empty or broken file (json.get_error_message() says why)
	return JSON.to_native(json.data)

What we measured

A save with 5 coins, day 12, a plot keyed by Vector2i(2, 3), an item keyed by the int 7, a 64-bit player ID 9007199254740993, a Vector2 spawn point and a list of seeds [3, 1, 4]. Two runs gave the same results.

Why it happens

JSON is a text format from the web. It has numbers but no separate whole-number type, and object keys must be text. JSON.stringify() writes 5 as 5, but JSON.parse_string() cannot tell it was an int, so every number comes back as a float. A key that is not text is written as its text, (2, 3) or 7, and comes back as that text. Vector2 and other Godot types are not JSON at all, so they are written as text too. from_native() wraps each value with its type, and to_native() reads the type back.

What to change

Effects for the farm itself: Cozy VFX for Godot 4 has 29 ready-made effects for farming and life games, including fireflies, sleeping Zzz, hearts, music notes, steam and falling leaves.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Cozy VFX for Godot 4 has 29 ready-made effects for farming and life games, including fireflies, sleeping Zzz, hearts, music notes, steam and falling leaves.

More how-tos for Cozy VFX for Godot 4: Night-only effects when the night crosses midnight

More how-tos: Game systems