SaveManager saves and loads the game. It has three kinds of storage:
- Save slots keep the player's progress in their browser or desktop storage, so they're still there next time they play.
- Autosaves are snapshots kept in memory while the game runs, for going back a step. They're lost when the game closes.
- Game settings keep preferences such as text speed or language, separately from save slots.
A save holds every project variable, the current scene, and the characters' variables. It doesn't hold scene variables (this.vars) or values kept on this.
| Member | Description |
|---|---|
Save() |
Saves the game to a slot |
Load() |
Loads a slot and goes to its scene |
Exists() |
Whether a slot has a save |
ClearSlot() |
Deletes a slot's save |
CopySlot() |
Copies a save to another slot |
GetSlotInfo() |
When and where a slot was saved |
GetSlots() |
Every slot that has a save |
SaveAutoSave() |
Takes an autosave |
LoadAutoSave() |
Goes back to the last autosave |
HasAutoSave() |
Whether there's an autosave |
GetAutoSaves() |
Every autosave this session |
ClearAutoSave() |
Deletes every autosave |
GetGameSettings() |
Reads a game setting |
SetGameSettings() |
Changes a game setting |
SaveGameSettings() |
Saves the game settings |
Save slots
A slot is named by a number or a word, such as 1, 2 or "quick". Each slot holds one save. Saving to a slot replaces what was there.
Save
SaveManager.Save(slot: number | string = 1): void
Saves the game to a slot, replacing any save already in it.
| Parameter | Type | Default | Description |
|---|---|---|---|
slot |
number | string |
1 |
The slot to save to |
Example
onSavePressed(slot: number) {
SaveManager.Save(slot);
this.dom.saved_message.show();
},
Load
SaveManager.Load(slot: number | string = 1): boolean
Loads the save in a slot: it restores the project variables and characters, then switches to the scene the game was saved on.
Returns true if the save was loaded, and false if the slot is empty or its save can't be read. Nothing changes when it returns false.
How it works
The scene starts fresh, the same as any other scene switch.
Example
onContinuePressed() {
if (!SaveManager.Load(1)) {
SceneManager.SwitchScene("new_game"); // nothing to continue, so start a new game
}
},
Exists
SaveManager.Exists(slot: number | string = 1): boolean
Returns true if the slot has a save.
Example
onEnter() {
this.dom.continue_button.hidden = !SaveManager.Exists(1); // hide Continue when there's nothing to continue
},
ClearSlot
SaveManager.ClearSlot(slot: number | string): boolean
Deletes the save in a slot. There's no default slot, so a slot is never cleared by accident.
Returns false if the slot was already empty.
Example
onDeletePressed(slot: number) {
if (SaveManager.ClearSlot(slot)) log(`Deleted slot ${slot}`);
this.refreshSlotList();
},
CopySlot
SaveManager.CopySlot(from: number | string, to: number | string): boolean
Copies the save in one slot to another, replacing whatever is in to. The copy keeps the original's save time.
Returns false if from is empty. Copying a slot onto itself does nothing and returns true.
Example
onQuickSave() {
SaveManager.CopySlot("quick", "quick_backup"); // keep the previous quick save, in case this one goes badly
SaveManager.Save("quick");
},
GetSlotInfo
SaveManager.GetSlotInfo(slot: number | string = 1): { date: string; scene: string } | null
Returns when the slot was saved, as a date and time written for the player's language, and the name of the scene it was saved on. Returns null if the slot is empty.
Example
onEnter() {
const info = SaveManager.GetSlotInfo(1);
this.dom.slot_label.textContent = info ? `${info.scene}, ${info.date}` : "Empty";
},
GetSlots
SaveManager.GetSlots(): { slot: number | string; time: number; scene: string; state: Record<string, any> }[]
Returns every slot that has a save, sorted by slot. Each has:
| Field | Description |
|---|---|
slot |
The slot, as a number if it's made of digits, otherwise as text |
time |
When it was saved, in milliseconds, like Date.now() |
scene |
The scene it was saved on |
state |
The saved variables, for showing things like the player's name or progress |
A save that can't be read is left out, with a warning in the console.
Example
refreshSlotList() {
this.dom.slot_list.replaceChildren();
for (const save of SaveManager.GetSlots()) {
const row = document.createElement("div");
const when = new Date(save.time).toLocaleString();
row.textContent = `Slot ${save.slot}: ${save.state.playerName ?? "Unnamed"}, ${when}`;
this.dom.slot_list.appendChild(row);
}
},
Autosaves
Autosaves are kept in a list in memory. Each SaveAutoSave() adds one, and each LoadAutoSave() goes back to the newest and removes it, so they work like an undo history. The list is emptied when the game reaches its start scene after the splash screens, and lost when the game closes.
SaveAutoSave
SaveManager.SaveAutoSave(): void
Takes a snapshot of the project variables, characters and current scene, and adds it to the list.
Example
onStart() {
SaveManager.SaveAutoSave(); // a checkpoint at the start of every level
},
LoadAutoSave
SaveManager.LoadAutoSave(): void
Goes back to the newest autosave and removes it from the list: it restores the variables and characters, then switches to its scene. Does nothing if there are no autosaves.
Example
onUndoPressed() {
if (SaveManager.HasAutoSave()) SaveManager.LoadAutoSave();
},
HasAutoSave
SaveManager.HasAutoSave(): boolean
Returns true if there's at least one autosave.
Example
onEnter() {
this.dom.undo_button.hidden = !SaveManager.HasAutoSave();
},
GetAutoSaves
SaveManager.GetAutoSaves(): { time: number; scene: string; state: Record<string, any> }[]
Returns this session's autosaves, oldest first, each with when it was taken, its scene and its saved variables.
Example
onHistoryOpened() {
const scenes = SaveManager.GetAutoSaves().map((save) => save.scene);
log("Visited:", scenes.join(" > "));
},
ClearAutoSave
SaveManager.ClearAutoSave(): void
Deletes every autosave.
Example
onNewGame() {
SaveManager.ClearAutoSave();
SceneManager.SwitchScene("chapter_1");
},
Game settings
Game settings are stored separately from save slots, and aren't affected by loading or deleting a save. Audio volumes are kept by AudioManager instead.
GetGameSettings
SaveManager.GetGameSettings<T>(key: string, defaultValue?: T): T
Returns a game setting, or defaultValue if it hasn't been set.
Example
onEnter() {
this.dom.speed_slider.value = String(SaveManager.GetGameSettings("textSpeed", 30));
},
SetGameSettings
SaveManager.SetGameSettings(key: string, value: any, save = true): void
Changes a game setting.
| Parameter | Type | Default | Description |
|---|---|---|---|
key |
string |
The setting's name | |
value |
any |
Its new value. Anything that can be written as JSON | |
save |
boolean |
true |
Save all game settings straight away. Pass false when changing several, then call SaveGameSettings() once |
Example
onApplyPressed() {
SaveManager.SetGameSettings("textSpeed", Number(this.dom.speed_slider.value), false);
SaveManager.SetGameSettings("language", this.vars.language, false);
SaveManager.SaveGameSettings(); // one save for both changes
},
SaveGameSettings
SaveManager.SaveGameSettings(): void
Saves the game settings to the player's storage. Only needed after SetGameSettings(key, value, false).