Guides

A Godot save that survives a crash is three files, not one

Shelf-Made Tycoon is a logistics tycoon built in Flockbay. This is its save system as the project holds it today: what goes in the file, how the file is written so that a crash mid-save loses nothing, and what the game does with a save it cannot read.

Game
Shelf-Made Tycoon, a logistics tycoon built in Flockbay
Source
The game’s own project: scripts/main.gd, scripts/sim/shelf_sim.gd and tests/save_fixtures.gd. The test was run again for this page, headless, on Windows
Engine
Godot 4.7.2
Save format
One Dictionary per company, written as text, format version 1

The whole company is one Dictionary, written as text

The simulation has one function, to_save(), that returns everything about the company as a Dictionary: the clock, the cash, the racks, the trucks, the contracts, and the state of each random number generator, so a loaded game rolls the same dice it would have. The main script adds the player’s view of it under a view key (where the camera looked, its zoom and the game speed), and that is the save.

It is written with one call, file.store_string(var_to_str(data)), and read back with str_to_var. Those two functions write and parse any Godot value as text, so the file keeps Godot’s own types. The camera’s target in that view is a Vector3, and it comes back as one: the loader checks target is Vector3 and uses it as it is. JSON would have handed back an array of numbers, or a string to parse.

The project keeps one real save as a test fixture, a company on its fourth day with six contracts and four racks. It is 143,492 bytes of text, and it starts like this:

tests/fixtures/save_v1.save (first lines)
{
"car_rng": -3445049661876544666,
"carry": 0.049999999999560035,
"cars": [],
"cash": 12591,
"clients": {
"aurora": {
"banned": false,
"cargo": ["cold", "fragile"],
"colour": "46afcc",
"credit": "good",
"done": 0,

A save you can open in a text editor is a save you can debug. When a player’s company loads wrong, the answer is in a file anyone can read and compare with another.

The new save never overwrites the old one

The obvious way to save is to open the save file and write. If the game crashes, the disk fills or the power goes halfway through, that leaves half a file where the last good save was. Shelf-Made writes somewhere else first:

scripts/main.gd (excerpt)
static func _write_save(path: String, data: Dictionary) -> Error:
	var temp := path + ".tmp"
	var file := FileAccess.open(temp, FileAccess.WRITE)
	if file == null:
		return FileAccess.get_open_error()
	file.store_string(var_to_str(data))
	var written := file.get_error()
	file.close()
	if written == OK and FileAccess.file_exists(path):
		written = _move(path, path + ".bak")
	if written == OK:
		written = _move(temp, path)
		if written != OK and not FileAccess.file_exists(path) and FileAccess.file_exists(path + ".bak"):
			_move(path + ".bak", path)
	return written

Three steps, and the old save is never deleted before the new one is in place:

Whatever stops the game between two of those steps, at least one whole file is on disk. The .bak also stays after a good save, as the save before the last one.

The renames go through a small helper, _move, that tries up to five times, 50 ms apart. The project’s comment on it gives the reason: an antivirus or a backup tool that has just seen a file written may hold it open for a moment, and a rename fails while it does.

Loading takes the newest file that parses

With three files that may exist, loading is a question of order:

scripts/main.gd (excerpt)
func _save_source(path: String = "") -> Dictionary:
	_finish_saving(true)
	if path == "":
		path = save_path()
	if path == "":
		return {}
	var order := [path + ".tmp", path, path + ".bak"]
	if FileAccess.file_exists(path + ".tmp") and FileAccess.file_exists(path) \
			and FileAccess.get_modified_time(path + ".tmp") < FileAccess.get_modified_time(path):
		order = [path, path + ".bak", path + ".tmp"]
	for file in order:
		if not FileAccess.file_exists(file):
			continue
		var data: Variant = str_to_var(FileAccess.get_file_as_string(file))
		if data is Dictionary:
			return {"path": file, "data": data, "fallback": file != path}
	return {}

A .tmp is only left behind when moving it in failed, so it normally holds the newest state and is read first, then the save, then the .bak. The first one that parses to a Dictionary wins, and fallback tells the game it did not come from the save itself. The player is then told so: “Loaded the backup save”, with the day and the time it holds.

The exception in the middle was added later, on 2 November 2026 by the project’s history. The game syncs its saves through Steam Cloud, which can bring a newer save from another PC and set it beside a stale .tmp from a write that died here. A .tmp older than the save beside it is therefore read last, not first.

A save that cannot be read is kept, not replaced

When the save, its .tmp and its .bak all fail to parse, the game does not start a new company on top of them. It renames the file to <save>.unreadable (.unreadable.2 and on, if one is already kept) and the title card says “Your save could not be read. It is kept as” with the file’s name. Nothing the game writes later can land on it, and whoever helps that player still has the file to look at.

The same check runs before the first save of every session, so a file that does not parse never becomes the .bak that the next save would write over.

The format has a version number, and it is checked before anything loads

A save parsing as a Dictionary does not make it a save this build can load. Before the running game is touched, a static function looks at the data alone:

scripts/sim/shelf_sim.gd (excerpt)
static func read_save(data: Variant) -> Dictionary:
	var unreadable := Loc.tag("The save file could not be read.")
	if not data is Dictionary:
		return {"why": unreadable}
	var save: Dictionary = data
	var made: Variant = save.get("version", 1)
	if not typeof(made) in [TYPE_INT, TYPE_FLOAT]:
		return {"why": unreadable}
	if int(made) > SAVE_VERSION:
		# TRANSLATORS: why a saved game cannot be loaded: it was saved by a later version of the game than this one.
		return {"why": Loc.tag("This save is from a newer version of the game.")}
	if int(made) < SAVE_VERSION:
		save = _migrate(save.duplicate(true), int(made))
	for key in SAVE_KEYS:
		if not save.has(key) or not typeof(save[key]) in SAVE_KEYS[key]:
			return {"why": unreadable}
	for row in save.trucks:
		if not row is Dictionary or not (row as Dictionary).has("path"):
			return {"why": unreadable}
	return {"data": save, "why": {}}

A save from a newer version of the game is refused with a sentence that says so. An older one goes through _migrate, which brings it up one version at a time; the format is still at version 1, so that function has no steps yet, but the place for them exists. Then every key the loader reads without a default is checked against the types it may hold (SAVE_KEYS). A save that fails any of this leaves the company on screen exactly as it was.

One old save is kept as a test, and the test still passes

The fixture above is there for one reason: every format the game has shipped keeps one real save in tests/fixtures/, with a small JSON file beside it that says what the company held. tests/save_fixtures.gd loads each one into the game as it is now, compares the day, the cash, the contracts and the racks, and runs the company on for an hour of game time. We ran it for this page, headless, on Windows:

What Godot printed, after its version line

FIXTURE save_v1.save (version 1): { "day": 4, "cash": 12591, "contracts": 6, "racks": 4 }, expected { "cash": 12591.0, "contracts": 6.0, "day": 4.0, "racks": 4.0, "version": 1.0 }; ran on to 01:00

FIXTURES_PASS

The expected numbers print as 12591.0 because they come from JSON, which has one number type; the game’s own numbers stayed integers through var_to_str. The test compares them as integers.

What this test does not check is a save written by a format that does not exist yet. When the format changes, the change needs its step in _migrate and a new fixture, and nothing in the project forces either.

The autosave writes on a worker thread, and a test run never saves

The game saves at every in-game midnight. The midnight save does its file work off the main thread, so that, in the words of the project’s own comment, a midnight on a grown yard never waits on the disk:

scripts/main.gd (excerpt)
func save_game_soon() -> void:
	var path := save_path()
	if path == "" or not _saved_once:
		save_game()
		return
	_finish_saving(true)
	# The copy shares no row with the company, which the clock moves on while the worker writes.
	var data := _save_data().duplicate(true)
	var out := {}
	var task := WorkerThreadPool.add_task(func(): out.written = _write_save(path, data), true)
	_saving = {"task": task, "out": out, "version": sim.version}

The frame only makes a deep copy of the Dictionary, duplicate(true), because the simulation keeps changing the original while the worker writes. _write_save is a static function that touches nothing but files, which is what makes it safe to run on another thread. Every other use of the save (a load, the list of companies, a quit) first waits for that task to finish.

Quitting saves too. The project turns off Godot’s automatic quit with get_tree().set_auto_accept_quit(false), so the window’s close button, Alt+F4 and the Quit buttons all arrive at one function that saves first. If that save fails, the game stays open and asks before it closes.

One guard matters when an AI builds the game. Flockbay’s AI checks its work by running the game headless, with no window, over and over. A save system that fired on those runs would write test companies over the player’s own. Shelf-Made only saves a game a person has had in front of them:

scripts/main.gd (excerpt)
## True once the game is in a window a person can see: never on the headless display server.
func _shown_to_person() -> bool:
	if DisplayServer.get_name() == "headless":
		return false
	return _seen_by_person or get_window().has_focus()

Each company is its own file

The full game keeps up to ten companies, each in its own save named for the moment it began, company-<unix time>.save, at the root of the game’s user folder. The list on the title card is built by reading that folder, and a company’s row comes from the data in its save: its name, how it started, its day and its cash.

Shelf-Made Tycoon’s Saved companies card over the warehouse yard: one company, Harbour Lane, From the ground up, Day 1, $18,000, last played 2026-11-25 09:43, with Load, Delete and Back buttons.
The Saved companies card, a frame the game rendered from a copy of the project with its own user folder. The row is read from the save file.

What to take from it

To build a game this way, download Flockbay and describe it; Getting started covers the first build. When you ask for saving, ask for the crash case by name.

More guides