Saving and loading
Save a game, load it back, and keep old saves working after you patch your game. Everything a game keeps in the world (components, timers, sequences, computer players, the score) goes into a save by itself.
Quick start
import { loadWorld, saveWorld } from '@v12-engine/engine'
const save = saveWorld(world) // plain data: store it anywhere
const again = await loadWorld(save, { rules: myGame })
export const myGame: Rules = { id: 'my-game', /* … */ saves: {} } // load saves made before a patchsaveWorld gives the whole game as plain data. saves: {} tells the engine that small patches (a number tuned, a bug fixed) should still load old saves.
Common tasks
Keep saves working when you rename or reshape things
export const myGame: Rules = {
id: 'my-game',
// …
saves: {
version: 3,
migrate: {
// from version 1 to 2: the knight became a paladin
1: (save) => {
for (const entity of save.entities) if (entity.type === 'knight') entity.type = 'paladin'
},
// from 2 to 3: the banner has several colours
2: (save) => {
const banner = save.state['my-game.banner'] as { colour: string }
save.state['my-game.banner'] = { colours: [banner.colour] }
}
}
}
}Bump version and add a step from the version before. Steps run one at a time (1, then 2). A version with nothing to change still needs a step, (save) => save.
Refuse saves made with other rules
saves: { onMismatch: 'refuse' }
saves: { onMismatch: (save, hash) => (save.tick < 100 ? 'refuse' : 'load') }Check a save before loading it
const problem = saveProblem(saveHeader(save), registry) // whether it loads, by its header
const updated = updateSave(save, registry) // the save as the game reads it now, or why notHandle a save that will not load
try {
await loadWorld(save, { rules: myGame })
} catch (error) {
if (error instanceof SaveError) console.log(error.reason) // 'newer', 'noWay', 'rules', 'missing', 'game', 'notASave', 'ground'
}Launch a page from a save
launch({ /* … */ from: save })Test every version of your saves
Keep a save of every version among your game's tests, and load them all with the game of now. See Testing your rules.
Reference
What a save says about its age
| Field | Whose | What it is |
|---|---|---|
format | Engine's | The shape of the save; the engine updates it by itself on load |
version | Game's | What your game keeps (types, components); only your migrate steps can update it |
hash | Game's | The exact rules it was made with (registry.hash): changes with any change of code, numbers or mods |
Load rules
- A game that declares
saves(even{}) loads saves made before a patch. A game that says nothing loads only saves made with its exact rules: right for tests, wrong for players. Declare it before anyone plays. - A step gets the save as plain data in today's format, as a copy: change it in place or return another.
- A version with no step after it was given up on; its saves are refused.
- A save of the same version made with other rules loads as is (
onMismatch: 'load', the default). - Anything the save has that the game no longer has (unit types, abilities, buffs, items, upgrades, orders, components, timers, sequences) is found before the world is made and reported all at once. Fix it with a step.
- A save from a newer version of the game is refused, never guessed at.
- Scenarios kept as saves (
*.scenario.json) are read the same way. Mods have no steps of their own; the game they are on has them.
| Declared | In code |
|---|---|
saves: { version, migrate: { 1: … } } | migrate: (save, from, to) => …, one function for all |
onMismatch: 'load', 'refuse' | onMismatch: (save, hash) => …; loadWorld(save, { rules: game, anyVersion }) |
loaded with loadWorld, launch({ from }) | updateSave(save, registry), saveProblem(saveHeader(save), registry) |
SaveError
reason is one of newer, noWay, rules, missing (with what is missing by kind), game, notASave, ground.
The ground in a save
A game that ships its maps (maps of the module, not in its hash) saves only the map's name, a hash of its ground, and the cells changed since (format 3). A 1024-cell map's ground is about 15 MB; this is a few bytes. On load the map is found by name and ground hash; a changed map is refused (SaveError with reason: 'ground', words save.ground). Pass maps in code with loadWorld(save, { maps }) or connect(transport, { maps }). A world on bare ground or an unshipped map saves all of it. A player joining a server without the map is sent its ground once, in parts.
What must be plain data
Everything a module puts in the world (components, order state, timer and sequence data) must be numbers, text, lists and plain objects, with entities by id. A Map, a Set or an entity makes the save throw, naming the field. After a load, two places hold equal copies, not the same object. A component kept in another form while the game runs (a Map, bytes) says how it is saved with save and load, on an entity, a player or the whole game: see Components.
Related
- Saves in the client (slots, autosave, checkpoints): Missions and the UI.
- Components, prefabs and changing the world: Components and world state.
- Mods and data changes: Mods and content packs.