Start your own UI
Put a game on screen with a UI of your own in plain JS, React or Vue, from the runner's snapshot and the player's commands.
Use these pages when you render the game with code of your own, or when you place the React UI's parts in a layout of your own. Start from the template for your framework (vanilla, react or vue), made as Create a game shows. Choosing a UI compares the ways to put a game on screen.
Every example on these pages comes in three tabs:
- Plain JS is DOM code on the runner, as in the
vanillatemplate'ssrc/main.ts. Each part is a functiondraw<Thing>(parent, snapshot)that importsrunner,addanddrawChoicesfrom./runner.ts. - React uses the React UI,
@ifloom/react. It places the ready-made part for the feature in a layout, or builds a component of your own with the React UI's hooks. - Vue is a component with no props that reads
view,snapshotandrunnerfrom./runner.ts, as thevuetemplate's components do.
The site remembers the tab you choose on every page. Every example renders the showcase, Small Town (examples/showcase), so it uses the showcase's ids, such as the vendor counter and the timer flat.ring.
Start the runner
Make the runner once, start the browser helpers beside it, render its snapshot, and render again on every change. The React UI's mountGame does all of it in one call.
// src/main.ts: the vanilla template's start, cut down to the loop.
import type { RenderedChoice, RunnerSnapshot } from '@ifloom/engine'
import { startAudio } from '@ifloom/audio'
import { browserRunner, startClock, startKeys } from '@ifloom/browser'
import module from './game.ts'
// The game module builds the game, and lists what the loader found wrong with it.
const { game, problems } = module.build()
if (problems.length) console.warn(problems)
// A new game with a random seed, undo, saves in localStorage and an autosave each turn.
const runner = await browserRunner(game)
// Each helper follows the runner by itself. startAudio plays view.audio from the first click or key press,
// startClock ticks the timers and minigames, and startKeys presses the choices on 1 to 9.
startAudio(runner)
startClock(runner)
startKeys(runner)
const root = document.querySelector<HTMLElement>('#root')!
/** An element with a class and some text, added to `parent`. The text goes in as textContent, so prose can't add markup. */
function add<K extends keyof HTMLElementTagNameMap>(parent: HTMLElement, tag: K, text = '', className = ''): HTMLElementTagNameMap[K] {
const element = parent.appendChild(document.createElement(tag))
if (text) element.textContent = text
if (className) element.className = className
return element
}
/** Buttons that send their commands, each greyed out with its reason when it can't be pressed. */
function drawChoices(parent: HTMLElement, choices: RenderedChoice[]): void {
for (const choice of choices) {
const button = add(parent, 'button', choice.label)
button.disabled = Boolean(choice.disabled)
if (choice.disabled) button.title = choice.disabled
button.onclick = () => runner.apply(choice.command)
if (choice.disabled) add(parent, 'p', choice.disabled, 'reason')
}
}
/** The whole screen, rendered again from each snapshot. */
function draw({ view }: RunnerSnapshot): void {
root.replaceChildren()
add(root, 'h1', view.title)
if (view.scene.title) add(root, 'h2', view.scene.title)
for (const beat of view.scene.beats) if (beat.type === 'text' || beat.type === 'say') add(root, 'p', beat.text)
// The open scene's choices, or the room's actions while no scene is open.
drawChoices(add(root, 'div', '', 'choices'), [...view.scene.choices, ...view.actions])
add(root, 'p', view.stats.map((stat) => `${stat.label}: ${stat.text}`).join(' · '), 'stats')
}
// Render again whenever the runner hands over a new snapshot.
runner.subscribe(draw)
draw(runner.snapshot())
A UI talks to the engine through three members of the Runner:
runner.snapshot()returns theRunnerSnapshot, one value with all a UI renders. It holds the view, the settings, the backlog, undo and the saves.runner.apply(command)runs a command, such as thecommandof the button the player pressed.runner.subscribe(listener)callslistenerwith each new snapshot, and returns the function that stops it.
The runner makes a new snapshot whenever any part of it changes, and returns the same object until then. A framework that compares values therefore renders again exactly when something is new. React reads it with useSyncExternalStore(runner.subscribe, runner.snapshot), and the Vue tab keeps it in a shallowRef. In Vue, src/main.ts then mounts the app with createApp(App).mount('#root').
browserRunner gives the runner a random seed, undo 50 steps deep, saves in localStorage and an autosave each turn. Pass it any RunnerOptions to change those, such as browserRunner(game, { backlog: true }). Each helper follows the runner on its own, and returns the function that stops it:
startAudioplays the game's music and sounds. Browsers allow no sound before the player clicks or presses a key, so nothing plays until then.startClocksends atickabout every 100 ms while a timer or a minigame's timer runs. The engine keeps no clock of its own.startKeyspresses the choices on screen on the keys 1 to 9, and a minigame's inputs on its keys.
Each template's vite.config.ts already adds its framework's Vite plugin to the Vite preset, which knows no framework.
Put the parts together
Split the screen into parts as it grows, one for each feature the other pages render. Each part reads the snapshot and sends its own commands, so the parts don't depend on each other.
import type { RunnerSnapshot } from '@ifloom/engine'
// add, as in the vanilla template's src/main.ts. Each part below imports runner, add and drawChoices from there too.
import { add } from './runner.ts'
import { drawActions } from './actions.ts'
import { drawEntities } from './entities.ts'
import { drawInputs } from './inputs.ts'
import { drawNotices } from './notices.ts'
import { drawPanel } from './panel.ts'
import { drawScene } from './scene.ts'
import { drawStats } from './stats.ts'
/** The screen, put together from the parts on the Render it yourself pages: a side column, the story, the notices and any open panel. */
export function drawLayout(parent: HTMLElement, snapshot: RunnerSnapshot): void {
parent.replaceChildren()
const side = add(parent, 'aside', '', 'sidebar')
add(side, 'h1', snapshot.view.title)
drawStats(side, snapshot)
drawEntities(side, snapshot)
const story = add(parent, 'main', '', 'story')
drawScene(story, snapshot)
drawInputs(story, snapshot)
drawActions(add(story, 'div', '', 'actions'), snapshot)
drawNotices(parent, snapshot)
drawPanel(parent, snapshot)
}
In Plain JS, move the runner, add and drawChoices from src/main.ts into src/runner.ts and export them. Then call drawLayout(root, snapshot) from the listener. In React, pass the layout to mountGame(module.build(), { layout: Layout }), and Arrange the layout moves and folds its sections. In Vue, render <Layout /> from src/App.vue.
The other pages render one feature in each section:
- Render the scene and its choices covers the scene, flowing scenes, the room's actions, inline choices and tooltips, inputs, and transitions.
- Render stats, things and what changed covers stats, the things the player holds, listed entities, changes, notices and dice rolls.
- Render pictures and play sound covers layered pictures with hotspots, and the audio player.
- Render timers, minigames and panels covers countdowns, a running minigame and a panel over the story.
- Render saves, settings and the backlog covers a load screen, a settings screen, the backlog and undo.
Each plugin adds its own part of the view under view.plugins, by plugin id, such as the places plugin's ways out at view.plugins.places.exits. The plugins' pages in this section render them: the calendar, locations and people, quests, achievements and statuses, the inventory, shop, jobs and wardrobe, messages, the feed and devices, and cheats, unlocks and debug.
For every field and member, see:
- The view and
View. - The runner.
- Commands.
- Browser helpers and the audio player.
Last updated on