Save the game so that a crash in the middle never costs the whole city
If the game closes while it is writing the save, the file is cut off and the player loses hours of building. Here is a way to keep two copies, check each for damage when loading, take the newest good one, and never overwrite the only good copy.
Why this works
Any write can be cut off, so the safe rule is never to touch the last good copy while writing a new one. Two slots taken in turn, each with a fingerprint and a counter, let the game spot damage and pick the newest copy that is still whole.
When not to use it
More than needed for a short game that plays out in a minute or two, and no help when the whole device fails or the player clears the browser data; for that, save a copy outside the device. It also protects against a cut-off file, not against a wrong state being saved.
In the full entry
- What to build, step by step
- Starting values and the reason for each
- 4 common mistakes and the fix
- 41 lines of example code with 4 checks that pass
- How your AI shows you that it works
Say this to your AI
Use the Spielerando entry "Save the game so that a crash in the middle never costs the whole city" (read_entry autosave-two-slots-with-checksum) in my game. Build it step by step, and show me how you checked that it works.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP