After a patch, players' save games fail to load or load with wrong values; how do you design an engine's saves so old files survive patches?
answer
- never dump memory layout
- version per type, not per file
- stable IDs for placed objects
- upgrade steps chained on load
- a corpus of old saves
basics
~20 sSave games survive patches when they store tagged fields per object, a version per type, and stable persistent IDs instead of pointers, then run chained upgrade steps on load and test every shipped version's saves automatically.
solid answer
~50 sSaves break when they mirror one build: raw struct bytes, positional fields, pointers or array indices, enum numbers. A durable design writes a **header** (format version, game build), then one record per object keyed by a **persistent ID** that the level and spawner assign and never reuse. Each record carries its **type's version** and fields **tagged by name or stable ID**, usually driven by reflection metadata. On load, missing fields take defaults, unknown ones are skipped, renames go through aliases, and anything with changed meaning runs through **upgrade functions** chained from the saved version to the current one. References are stored as persistent IDs and resolved in a second pass. Content a patch removed maps to a fallback. Finally, keep a **corpus of saves from every shipped build** and load all of them in automated tests on every change.
code
pseudocode · 19 linesfunction load_record(record):
type = types[record.type_name]
if record.version > type.current_version:
fail("save written by a newer build")
data = record.fields # map of field name -> value
v = record.version
while v < type.current_version:
data = type.upgraders[v](data) # rewrites version v into version v + 1
v = v + 1
obj = type.construct_default()
for field in type.saved_fields:
name = field.name
if name not in data and field.old_name in data:
name = field.old_name # rename alias
if name in data:
field.set(obj, data[name])
# missing field: keeps its default; unknown names in data are ignored
pending_refs.add(obj, record.reference_ids) # resolved after all records load
return objgo deeper
Know that a save written as raw memory breaks when the code changes, and that saves need a version number.
Explain tagged fields with defaults for missing ones, persistent IDs for objects, and why pointers and enum ordinals cannot be saved.
Design per-type versions with chained upgrade steps, two-pass reference resolution, removed-content fallbacks and a save corpus run in automated tests.
Decide how many shipped versions the team supports, whether to save deltas against authored levels, and who owns save compatibility in each patch.
## Why saves break after a patch A save game is a snapshot of world state written by one build and read by a later one. It breaks when it records details of the **writing build** instead of the **game state**: - **raw memory**: copying a component's bytes ties the file to field order, sizes and padding, so adding one field shifts everything after it; - **positional fields**: writing values in declaration order without tags means a reordered or inserted field reads its neighbour's value; - **pointers and indices**: a pointer is meaningless in the next process, and an index into an array changes when the array's content changes; - **enum numbers**: inserting a new value in the middle of an enum renumbers the ones after it; - **removed or renamed content**: an item, quest or level the save refers to no longer exists under that name. (General schema-compatibility rules, such as why adding a field with a default is safe, belong to serialization as a whole; what follows is what an engine's save system has to add.) ## The shape of a durable save 1. A **header**: a magic number, a **save-format version** for the container itself, the game build that wrote it, and a checksum. 2. One **record per saved object**, keyed by a **persistent ID**. Objects placed in a level get an ID when they are authored, and spawned objects get one from a counter stored in the save; IDs are never reused. 3. Each record names its **type** and that **type's version**, so changing a door component bumps only the door's version. 4. Fields are **tagged** by name or stable field ID, with a length so a reader can skip what it does not know. Reflection metadata usually drives this: every field flagged `saved` is written by name. 5. **References** to other objects are stored as persistent IDs and resolved after every record is loaded, in a second pass. 6. **Enums and content** are written as stable names or IDs, not ordinal numbers or file paths. ## Loading an older save | Change in the patch | How the loader copes | |---|---| | Field added | Missing in old saves, so it keeps the type's default | | Field removed | Present in old saves, skipped by its tag and length | | Field renamed | An alias in the metadata maps the old name to the new one | | Meaning changed, such as health 0-100 becoming 0-1 | An upgrade function for that type rewrites the value | | Item or quest removed | A mapping table replaces it with a fallback, or refunds it | | Placed object deleted from the level | Its record has no target; the loader drops it and logs a warning | **Upgrade functions** are the key to semantic changes. Each type keeps a function per version step, such as door version 2 to 3, and the loader applies them in order from the saved version up to the current one. Each step is small and written once, when the change is made, and old steps stay in the code for as long as old saves are supported. A save whose version is **newer** than the build reading it, after a rollback or a mismatched patch, should be refused with a clear message rather than guessed at. ## Saving less The smallest save is the safest. Many games save **deltas against the level as authored**: which placed objects were destroyed, opened or moved, plus everything spawned at run time. Untouched objects come from the shipped level data, so a patch that fixes a placed object's position reaches existing saves. That only works because placed objects carry stable persistent IDs. ## Proving it works - Keep a **save corpus**: saves from each shipped build, from several points in the game, including edge cases such as mid-quest and mid-cutscene states. - Load every save in the corpus in **automated tests** on each change, and compare key state such as inventory, quest flags and position against expected values. - Write saves **atomically**: write to a temporary file, then rename over the old one, so a crash or power loss mid-write leaves the previous save intact.
- Why give each type its own version number instead of one version for the whole save file?A single file version forces every upgrade step to know about every type, and one change anywhere bumps everything. Per-type versions keep each upgrade local to the type that changed; the file-level version then only covers the container format itself.
- How do saved references between objects survive a load when objects are created in arbitrary order?Store each reference as the target's persistent ID. Load every record first, building a map from persistent ID to the new object or handle, then make a second pass that resolves each stored ID through the map, dropping and logging any that point at an object that no longer exists.
- Why write the save to a temporary file and rename it rather than overwriting in place?Overwriting in place can leave a half-written, corrupt file if the game crashes or the device loses power mid-write. A rename over the old file replaces it in one step on most file systems, so the player keeps either the old save or the new one.
- Bump one global save version and reject all older saves
- Copying component structs straight to disk is fine
- Saved references can be pointers or array indices
- Old upgrade steps can be deleted after each patch
- Enums can be saved as their ordinal numbers