Render saves, settings and the backlog
Render a save and load screen, a settings screen and the backlog with Undo in a UI of your own, in plain JS, React or Vue.
Render the screens a player opens from a menu: saves to load, settings to change, and the backlog of what was shown, with Undo. Set up the runner first, as Start your own UI shows. These parts read the runner's snapshot as well as its view, because saves, setting values, the backlog and undo belong to the runner rather than to the game state.
Render a save and load screen
snapshot.saves lists the saves in the runner's save store, newest first, then any that can't be read. Each is a SaveMeta with its name, or { slot, broken } with why it can't be read. The React UI's GameMenu opens its SavesMenu, which you can also place yourself, and useSaves does everything it does without its look.
import type { RunnerSnapshot } from '@ifloom/engine'
import { nextSlot } from '@ifloom/browser'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'
/** A Save button, greyed out while the game refuses saving, and a row for each save with Load and Delete. */
export function drawSaves(parent: HTMLElement, { view, saves }: RunnerSnapshot): void {
const { allowed, reason, visible } = view.saving
if (visible) {
const save = add(parent, 'button', 'Save')
save.disabled = !allowed
if (reason) save.title = reason
// Each save in a slot of its own, '1', '2', ..., named from view.when, such as "Day 2 · Tuesday morning · The Corner Café".
save.onclick = () => void runner.save(nextSlot(saves))
}
// The snapshot lists the saves, and the runner hands a new one after every save and delete.
const list = add(parent, 'ul', '', 'slots')
for (const entry of saves) {
const row = add(list, 'li')
// A save that can't be read says why, and can only be deleted.
add(row, 'span', 'broken' in entry ? entry.broken : entry.name)
if (!('broken' in entry)) add(row, 'button', 'Load').onclick = () => void runner.load(entry.slot)
add(row, 'button', 'Delete').onclick = () => void runner.saves?.remove(entry.slot)
}
}
Save with runner.save(slot), in a new numbered slot from nextSlot, which skips named slots such as auto. Load with runner.load(slot), and delete through the store with runner.saves.remove(slot). Each is async and answers a Result rather than throwing, so show its reason when it fails. The runner keeps snapshot.saves up to date, and a load replaces the game in play and calls your listener, so the screen renders again without code of yours.
view.saving says whether the game allows saving now, as a SavingView. Grey Save out while allowed is false, with reason as its title, and leave it off the screen while visible is false. A Continue button loads the first save in the list that isn't broken, the autosave included. Write a save driver keeps saves somewhere other than localStorage.
Render a settings screen
view.settings lists every setting the game declares, in order, as a SettingView with its label, its type, its default, and the options of a choice or the range of a slider. The React UI renders a Settings page when you turn on its pages, and useSetting reads one setting for a component of your own.
import type { RunnerSnapshot } from '@ifloom/engine'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'
/** A settings screen: every setting the game declares, with its value now. A change goes through runner.setSetting. */
export function drawSettings(parent: HTMLElement, { view, settings }: RunnerSnapshot): void {
// settings has the values of both kinds: play settings from the state, ui settings from the profile.
for (const setting of view.settings) {
const label = add(parent, 'label', setting.label)
const value = settings[setting.id] ?? setting.default
if (setting.type === 'toggle') {
const box = add(label, 'input')
Object.assign(box, { type: 'checkbox', checked: value === true })
box.onchange = () => runner.setSetting(setting.id, box.checked)
} else if (setting.type === 'choice') {
const select = add(label, 'select')
for (const option of setting.options ?? []) Object.assign(add(select, 'option', option.label), { value: option.value, selected: option.value === value })
select.onchange = () => runner.setSetting(setting.id, select.value)
} else {
const range = add(label, 'input')
Object.assign(range, {
type: 'range',
min: String(setting.min ?? 0),
max: String(setting.max ?? 100),
step: String(setting.step ?? 1),
value: String(value),
})
range.onchange = () => runner.setSetting(setting.id, Number(range.value))
}
}
}
Read each setting's value from snapshot.settings, which holds both kinds. A play setting is part of the game state, and the profile keeps a ui setting, never the state. Change either kind with runner.setSetting(id, to), which checks the value against the declaration and answers a Result. A ui setting does only what your UI makes of it, so copy it where your stylesheet can read it, such as document.documentElement.dataset.textSize. The runner calls your listener when a setting changes, so the screen renders again.
Render the backlog and undo
snapshot.backlog lists what was shown, oldest first, each a BacklogEntry with its title, its beats and the choice the player made. snapshot.canUndo and snapshot.canRedo say whether runner.undo() and runner.redo() have a step to take. The React UI's GameMenu offers the backlog and Undo when the runner has them, and Backlog renders the backlog wherever you place it.
import type { RunnerSnapshot } from '@ifloom/engine'
// runner and add, as in the vanilla template's src/main.ts. The runner's backlog is on: browserRunner(game, { backlog: true }).
import { add, runner } from './runner.ts'
/** What was shown, oldest first, each scene's choice after its words, and a button that steps back one command. */
export function drawBacklog(parent: HTMLElement, { backlog, canUndo }: RunnerSnapshot): void {
const undo = add(parent, 'button', 'Undo')
undo.disabled = !canUndo
undo.onclick = () => runner.undo()
const list = add(parent, 'ol', '', 'backlog')
for (const entry of backlog) {
const item = add(list, 'li')
add(item, 'h3', entry.title)
for (const beat of entry.beats) if (beat.type === 'text' || beat.type === 'say') add(item, 'p', beat.text)
if (entry.chose) add(item, 'p', entry.chose, 'chose')
}
}
The backlog stays empty unless the runner has it on, with browserRunner(game, { backlog: true }). The backlog keeps inline actions as plain words, so nothing in it can be pressed. browserRunner turns undo on, 50 steps deep, and runner.offers says which of the two a menu should offer.
For more on what these sections render, see:
- Saves, Settings, The profile and The runner.
- Browser helpers, for
browserRunnerandnextSlot. - Add a settings page and Write a save driver.
Last updated on