Game save and persistence

From makifbaysal/tasktrooper · View interactive version

Game save and persistence is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when adding or changing save games, settings or progression storage - a versioned save format with a tested migration chain, atomic writes with backup and checksum, per-engine storage locations, safe serializers, and the round-trip/migration/corruption tests.

How to use this skill

Skill file

Game Save and Persistence

Overview

A save file outlives the code that wrote it. Players carry saves across updates, crashes happen mid-write, and a lost 40-hour save is the bug players remember. Every save has a version, every older version has a tested migration, and a write never destroys the last good save.

Core principle: Versioned, migrated, written atomically, checked on load — and never deserialized with a format that can execute code.

The format

{
  "version": 3,
  "savedAtUtc": "2026-10-08T12:00:00Z",
  "player": { "levelId": "forest_02", "position": [12.5, 0, -4.0], "hp": 72 },
  "inventory": [{ "itemId": "sword_iron", "count": 1 }],
  "flags": ["met_blacksmith", "boss_forest_defeated"]
}

Migrations

type Migration = (save: any) => any;
const migrations: Record<number, Migration> = {
  1: (s) => ({ ...s, version: 2, inventory: s.items.map((id: string) => ({ itemId: id, count: 1 })) }),
  2: (s) => ({ ...s, version: 3, flags: Object.keys(s.questState ?? {}).filter((k) => s.questState[k] === "done") }),
};

export function upgrade(raw: any): SaveV3 {
  if (typeof raw?.version !== "number") throw new SaveError("unversioned");
  if (raw.version > CURRENT) throw new SaveError("from a newer game version");  // refuse, never guess
  let s = raw;
  while (s.version < CURRENT) s = migrations[s.version](s);
  return SaveV3Schema.parse(s);
}

Writing safely

  1. Serialize to bytes in memory.
  2. Write to save.json.tmp, flush to disk.
  3. Keep the previous file as save.json.bak.
  4. Rename the temp file over save.json (atomic on the same volume).
  5. Store a checksum (CRC32 or SHA-256 of the payload) in a header or a sidecar.

On load: verify the checksum and parse; on failure fall back to .bak, tell the player, and never crash or silently start a new game over the old one.

Serialize on the main thread if the engine requires it, but do file I/O off it (or between frames at a checkpoint) — a save must not hitch gameplay. Autosave at checkpoints, not every frame.

Where saves live

| Engine | Location / API |

|---|---|

| Unity | Application.persistentDataPath + System.IO; PlayerPrefs only for tiny settings |

| Godot | user:// with FileAccess; ConfigFile for settings |

| Unreal | USaveGame subclass + UGameplayStatics::AsyncSaveGameToSlot / LoadGameFromSlot; keep a version UPROPERTY in it |

| Web | IndexedDB for saves (async, larger; e.g. idb-keyval), localStorage only for small settings — it is synchronous, ~5 MB and can throw in private mode or when full |

| Bevy / Rust | serde to the platform data dir (directories crate) |

| pygame / MonoGame | the OS user data directory, never the install folder |

Consoles and some stores impose their own save APIs and size limits — follow the platform layer the repository already has.

Serializers that execute code — never for save data

Tests

Common Mistakes

Red Flags

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.