Add IFLoom to an existing project
Put an IFLoom game in a web project that already has its own build, rendered into one of its pages.
Use this to put a game in a project that already has its own build, such as a site or an app. You need Node 20.19 or later, or 22.12 or later. To start a game in a new folder, see Create a game instead.
Install the packages
Install the engine and the browser helpers, and the CLI as a dev dependency:
npm install @ifloom/engine @ifloom/browser
npm install --save-dev @ifloom/cliAdd @ifloom/react too for the ready-made React UI, and @ifloom/audio for sound with a UI of your own. A plugin is one more package, as Add a plugin shows.
Write the game module
Make src/game.ts, the game module, whose default export defineGame makes. Its build returns what loadGame returns, and the page and the CLI both build the game from it:
// src/game.ts
import { loadGame, text, type Content } from '@ifloom/engine'
import { defineGame } from '@ifloom/cli/game'
const opening: Content = {
introScene: 'door',
scenes: [{ id: 'door', title: 'The door', body: [text('A green door, half open. Someone inside is laughing.')], choices: [] }],
}
export default defineGame({
build: () => loadGame([opening], { id: 'my-game', title: 'My Game' }),
})@ifloom/cli/game imports only the engine, so the page can import this module without bringing the CLI into the browser build. Split the content into files as the game grows (see Organise your game's files).
Render it into the page
Render the game into an element on your page, here <div id="game">. The React tab uses the React UI, and the others render the scene and its choices with code of your own:
// src/play.ts
import { browserRunner } from '@ifloom/browser'
import module from './game.ts'
const { game } = module.build()
const runner = await browserRunner(game)
const root = document.querySelector<HTMLElement>('#game')!
function render(): void {
const view = runner.view()
root.replaceChildren()
for (const beat of view.scene.beats) {
if (beat.type === 'text') root.append(Object.assign(document.createElement('p'), { textContent: beat.text }))
}
for (const choice of view.scene.choices) {
const button = Object.assign(document.createElement('button'), { textContent: choice.label, disabled: Boolean(choice.disabled) })
button.onclick = () => runner.apply(choice.command)
root.append(button)
}
}
runner.subscribe(render)
render()browserRunner starts a new game with undo, saves in localStorage and an autosave each turn. The imports keep their .ts extensions, as the templates' do. Drop them if your TypeScript settings don't allow it. Start your own UI renders the rest of the game with your own code.
Add the scripts
Add the CLI's commands to package.json:
{
"scripts": {
"check": "ifloom check",
"playtest": "ifloom playtest --turns 5 --out playtest/run.txt",
"typecheck": "tsc --noEmit"
}
}A project that builds with Vite can also use ifloom dev and ifloom build, with the Vite preset in its vite.config.ts. A project that builds with something else keeps its own build. See Build with Next.js, Build with webpack and Build with esbuild.
Check it worked
Run the checks from the project's folder:
npm run check
npm run typecheckcheck loads src/game.ts and prints No problems. when the game loads cleanly. Otherwise it lists each problem with the scene or key it's about. Then open the page: it shows "The door" and its words.
For more, see:
- The CLI, for the game module and every command.
- The runner and the view, for what the page reads.
- How the pieces fit, for the packages a game needs.
Last updated on