Render cheats, unlocks and debug
Render a cheat menu, a box for supporter codes and a debug readout in a UI of your own, in plain JS, React or Vue.
Render the tools that sit outside the story: the player's cheats and a box for codes, both from unlocks, and a readout for yourself from debug. Set up the runner first, as Start your own UI shows. In the React tabs, each plugin's UI comes from its /react subpath and goes in mountGame's plugins, as Show plugins in the React UI explains.
Render the cheats
view.plugins.unlocks.cheats holds the cheats the player can see, each a CheatView with its group, how often this save has used it, and a ready button. The React UI's unlocksUi puts a CheatsButton in the side column that opens the CheatPanel.
import type { RunnerSnapshot } from '@ifloom/engine'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'
/** The cheats under their groups, each a button that says how often it's been used, greyed out with why when it's refused. */
export function drawCheats(parent: HTMLElement, { view }: RunnerSnapshot): void {
const cheats = view.plugins.unlocks?.cheats ?? []
for (const group of new Set(cheats.map((cheat) => cheat.group))) {
if (group) add(parent, 'h3', group)
for (const cheat of cheats.filter((entry) => entry.group === group)) {
const button = add(parent, 'button', cheat.used ? `${cheat.name} (used ${cheat.used})` : cheat.name)
button.disabled = Boolean(cheat.button.disabled)
button.title = cheat.button.disabled ?? cheat.description
button.onclick = () => runner.apply(cheat.button.command)
}
}
}
Group the cheats by group, in the order the game declares them. A cheat's button has the reason it's refused in disabled, such as a supporter cheat before its code is entered, so render it greyed out with the reason. used counts the times this save has used the cheat, and the view's cheated counts them all. A cheat runs in any scene and through any lock, so the menu can stay on screen. useCheats gives the cheats to a React component of your own.
Render a box for codes
view.plugins.unlocks holds every unlock as an UnlocksView, each with whether the player has it, and the runner's snapshot holds where entering a code stands at helpers.unlocks, an UnlockStatus. The React UI's unlocksUi starts codes on the runner and puts the UnlocksPanel on the settings page with pages on, or behind an Enter a code button in the menu without them.
import type { RunnerSnapshot } from '@ifloom/engine'
import { enterCode } from '@ifloom/plugin-unlocks'
// runner and add, as in the vanilla template's src/main.ts. Start codes there once, beside startClock: startUnlocks(runner).
import { add, runner } from './runner.ts'
/** A code box while codes have started, with where the last code stands, and the unlocks with which the player has. */
export function drawUnlocks(parent: HTMLElement, { view, helpers }: RunnerSnapshot): void {
const unlocks = view.plugins.unlocks
if (!unlocks?.total) return
// What startUnlocks reports: idle, checking, wrong or unlocked. A new snapshot follows each change.
const codes = helpers.unlocks
if (codes) {
const form = add(parent, 'form')
const input = add(form, 'input')
input.placeholder = 'Code'
const button = add(form, 'button', codes.status === 'checking' ? 'Checking…' : 'Unlock')
button.disabled = codes.status === 'checking'
form.onsubmit = (event) => {
event.preventDefault()
void enterCode(runner, input.value)
}
if (codes.status === 'wrong') add(form, 'p', codes.reason ?? '', 'reason')
if (codes.status === 'unlocked') add(form, 'p', `Unlocked: ${codes.entered?.name}.`)
}
add(parent, 'h3', `Unlocked ${unlocks.unlocked} of ${unlocks.total}`)
const list = add(parent, 'ul', '', 'unlocks')
for (const unlock of unlocks.unlocks) {
const item = add(list, 'li', `${unlock.name}: ${unlock.unlocked ? 'unlocked' : 'locked'}`)
item.title = unlock.description
}
}
Entering a code isn't a command, because the plugin checks each code against a slow hash. Set it up in this order:
- Before you build the game, open the content the player's codes unlock with
unlockedPacks, from the game's id and its profile. Use the same save driver the runner will use, and pass itspacksto the game module'sbuildasunlocked. - After you make the runner, start taking codes with
startUnlocks(runner), besidestartClock. It records each remembered unlock in the game and reports inhelpers.unlocks. - Send each code the player enters to
enterCode(runner, code). When its result'srestartis true, the code opened content the game in play lacks, so reload the page to build the game again.
helpers.unlocks.status is checking while a code is checked, then wrong with its reason, or unlocked with what it entered. Disable the button while it's checking, since a check takes a fraction of a second per unlock on a phone. In React, unlocksUiWith takes the problems unlockedPacks returns and an onChange that reloads, as the React tab shows. useUnlocks gives the unlocks' view to a React component of your own.
Render a debug readout
view.plugins.debug holds a readout for the developer as a DebugView, with the locks that are on, how often each trigger has fired, and the save's seed. The React UI's DebugCard, from debugUi, goes at the end of the story.
import type { RunnerSnapshot } from '@ifloom/engine'
import { debug } from '@ifloom/plugin-debug'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'
/** What a debugger wants on screen: the save's seed, the locks that are on, the triggers fired, and a few commands that ignore locks. */
export function drawDebug(parent: HTMLElement, { view }: RunnerSnapshot): void {
const shown = view.plugins.debug
if (!shown) return
add(parent, 'p', `Seed ${shown.seed}`)
for (const lock of shown.locks) add(parent, 'p', `Locked: ${lock.reason} (${lock.deny.join(', ')})`, 'lock')
add(parent, 'p', `Triggers fired: ${Object.keys(shown.fired).join(', ') || 'none'}`)
add(parent, 'button', '100 coins').onclick = () => runner.apply(debug.stat('coins', 100))
add(parent, 'button', 'Close the scene').onclick = () => runner.apply(debug.scene())
}
The plugin's commands, such as debug.stat('coins', 100) and debug.scene(), ignore locks, so a button can send them at any time. A release build leaves the plugin out, so view.plugins.debug is undefined there and the readout renders nothing. That lets you keep it in your UI for every build, and DebugCard does the same. useDebug gives the debug view to a React component of your own.
For more on what these sections render, see:
- Unlocks and cheats and Debug.
- The runner, for the snapshot's
helpers. - Sell supporter content and Debug a game.
Last updated on
Render messages, the feed and devices
Render texts with replies, a feed of posts and a device with its apps in a UI of your own, in plain JS, React or Vue.
Set up the React UI
Add the React UI to a game with React, Tailwind, the TypeScript and Vite settings, its stylesheet and the entry that mounts the game.