Autosave
Autosave writes the player's progress by itself. It waits until the player has stopped changing things for a moment, but never longer than a set time, and only at a calm moment. Leaving the game saves at once, and a failed save is tried again after a wait that grows.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Autosave" to my game.
What it should do: Autosave means a crash, a dead battery or a closed window costs the player a minute of play and not an evening. The player never has to think about saving, and the game never freezes at a bad moment to do it.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> clean
clean: Nothing to save
dirty: Changes waiting
saving: Writing
retrying: Waiting to retry
clean --> dirty: player changes something
clean --> saving: change while leaving
dirty --> saving: calm and quiet or leaving
saving --> clean: write worked
saving --> dirty: worked, but more changed
saving --> retrying: write failed
retrying --> dirty: wait is over
retrying --> saving: player leaves
```
Use these starting values:
- settleTime: 3 s
- maxUnsaved: 60 s
- retryDelay: 5 s
- writeTimeout: 10 s
Watch out for these common mistakes:
- The player never stops moving: Tell your AI: besides the quiet wait, add a longest wait. When it runs out, save anyway, at the next calm moment.
- Saving in the middle of a fight: Tell your AI: only start a save at a calm moment, which the game decides, for example on the ground with no enemy near. When leaving, save anyway, because it may be the last chance.
- Changes during the save: Tell your AI: take a copy of the data when the save starts, and remember if anything changed after that. When the write ends, only mark the data saved if nothing changed meanwhile.
- A failed save looks like a good one: Tell your AI: only count a save as done when the write reports success. Write to a new file first and swap it in when complete, so the old good save stays intact. Try again after a wait.
- A save that never answers: Tell your AI: give every save a time limit. When it runs out, treat the save as failed and try again later.
- Retrying every tick: Tell your AI: after a failure wait a few seconds, and wait longer after each failure in a row, up to a limit.
- Saving when nothing changed: Tell your AI: only save when something changed since the last good save.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
Autosave means a crash, a dead battery or a closed window costs the player a minute of play and not an evening. The player never has to think about saving, and the game never freezes at a bad moment to do it.
How it runs, as a diagram
Everything it can be in, and what moves it on. Your AI builds from exactly this.
How it runs, as a diagram
- Nothing to save → Changes waiting: player changes something
- Nothing to save → Writing: change while leaving
- Changes waiting → Writing: calm and quiet or leaving
- Writing → Nothing to save: write worked
- Writing → Changes waiting: worked, but more changed
- Writing → Waiting to retry: write failed
- Waiting to retry → Changes waiting: wait is over
- Waiting to retry → Writing: player leaves
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| settleTime | 3 s | How long the player must stop changing things before a save starts. Saving after every single change would write constantly while the player is busy. A few seconds of quiet is a good time to write. |
| maxUnsaved | 60 s | The longest a change may wait for a save once the moment is calm. A player who never stops moving would otherwise never be saved. This is also the most play a crash can cost. |
| retryDelay | 5 s | How long to wait after a failed save before trying again. The wait is multiplied by the number of failures in a row, up to six times, so a full disk is not hammered every moment. It is never zero. |
| writeTimeout | 10 s | How long a save may run without an answer. A save that hangs would block every later save, so after this time it counts as failed. |
What usually goes wrong
The player never stops moving
If the game waits for a quiet moment and the player keeps changing things, the save never starts. A long session can then be lost at once.
Fix: Tell your AI: besides the quiet wait, add a longest wait. When it runs out, save anyway, at the next calm moment.
Saving in the middle of a fight
The save lands while the character is in the air, in a fight or on a loading screen. Loading it later puts the player in a moment that cannot be played, such as a fall that cannot be survived.
Fix: Tell your AI: only start a save at a calm moment, which the game decides, for example on the ground with no enemy near. When leaving, save anyway, because it may be the last chance.
Changes during the save
The player picks something up while the save is being written. If the game marks everything as saved when the write ends, that pickup is lost without any warning.
Fix: Tell your AI: take a copy of the data when the save starts, and remember if anything changed after that. When the write ends, only mark the data saved if nothing changed meanwhile.
A failed save looks like a good one
The disk is full or the write is cut off. The game thinks it saved and shows no warning, and the next time the player loads, an old or broken file is there.
Fix: Tell your AI: only count a save as done when the write reports success. Write to a new file first and swap it in when complete, so the old good save stays intact. Try again after a wait.
A save that never answers
The write hangs, for example because a cloud folder is offline. The game waits for the answer for ever and never starts another save.
Fix: Tell your AI: give every save a time limit. When it runs out, treat the save as failed and try again later.
Retrying every tick
A save fails and the game tries again at once, every tick. The game stutters and the disk is hammered, which often makes the failure worse.
Fix: Tell your AI: after a failure wait a few seconds, and wait longer after each failure in a row, up to a limit.
Saving when nothing changed
The game writes on a timer even when the player did nothing, for example while paused. This wears the disk and slows weak devices for no reason.
Fix: Tell your AI: only save when something changed since the last good save.
How to check it works
- The player changed something. nothing else changes: the save starts after exactly 3 seconds of quiet.
- The player changes something every tick. 60 seconds pass: the save starts at 60 seconds, not before and not never.
- There are changes and the moment is not calm for 150 seconds. the moment becomes calm: no save started before, and one starts on the first calm tick.
- There are changes and the moment is not calm. the player leaves: a save starts on that tick.
- Nothing has changed. the player leaves: no save starts.
- A save is running. the player keeps changing things and leaving: no second save starts.
- A save is running. it reports success: nothing is left to save and the count of good saves is 1.
- A save is running. the player changes something and then it reports success: the change is still waiting to be saved.
- A save is running. it reports success on the same tick the player changes something: the change is still waiting to be saved.
- A save is running. it reports failure: the data is still unsaved, no good save is counted, and the next try starts after exactly 5 seconds.
- Eight saves fail in a row. you look at the waits: they are 5, 10, 15, 20, 25, 30, 30 and 30 seconds.
- A save is running and gives no answer. 10 seconds pass: it counts as failed and a retry wait begins.
- One save has failed. the next one succeeds: the failures in a row are back to 0.
- No save is running. a success or failure report arrives: nothing is counted.
- A save has just failed and a retry wait is running. the player leaves: a new save starts at once.
- The player changed something. one very long tick of 5 seconds passes: the save starts on that tick.
Works well with
- Checkpoints: A marker is a calm, well-chosen moment. Treat it like a change, so the save follows within a few seconds. Ask for a save when a marker is touched.
- World State Flags: If flags and items are saved at different moments, a load can show a door opened but the key still in the pocket. Save the flags with everything else, in one write.
- Inventory System: Picking up, using and dropping items are the changes players hate to lose most. Count an item change as a change to save.
- Daily Rewards: If the game closes right after a reward, the claim must already be written, or the player can claim it again. Save before showing the reward.
Common questions
how do i add autosave to my game
Paste this: Add autosave. Mark the data as changed whenever the player changes something worth keeping. Save after 3 seconds without changes, or after 60 seconds at the latest, but only at a calm moment. Save at once when the player leaves. If a save fails, wait and try again.
how often should a game autosave
Not on a fixed timer. Save a few seconds after the player stops changing things, and no later than a minute after the first change. That keeps the loss small and the writes few.
why does my game freeze when it autosaves
The save runs on the same thread as the game and writes a big file. Take a copy of the data first, write it in the background, and start only at a calm moment. A short message such as a small icon tells the player it is happening.
what if the save file gets broken
Never write over the only good copy. Write a new file, check that it is complete, then swap it in. Keep the previous good file as a backup, so one cut-off write cannot destroy the whole save.
Where it comes from
We could not find a reliable origin for this one yet.
What was checked
- 20 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it