Branching conversations and narrative choices. Dialogue as data (nodes, speakers, lines, choices), conditions and effects with variables and flags, conditional branches, text interpolation, events that start quests or give items, seen-tracking, a runner for the UI, typewriter text with skip, a validator for broken links, unreachable nodes and dead ends, dialogue box UI for every device, and wri...
Download skill (.zip) Files on GitHubDialogue system
Drop-in: assets/dialogue.js (tests: interpolation, hidden conditional choices, effects and events, conditional branch
fallback, costs applied, validator finds an orphan node, typewriter and skip).
import { runDialogue, validateDialogue, Typewriter } from "./core/dialogue.js";
const BAKER = {
start: { speaker: "Baker", text: "Morning, {name}! Seen any rats?", choices: [
{ text: "I'll handle them.", effects: { set: { ratQuest: true } }, event: "acceptRats", next: "thanks" },
{ text: "Bread, please ({gold} gold)", if: (v) => v.gold >= 5, effects: { add: { gold: -5 } }, next: "bread" },
{ text: "Bye.", next: null } ] },
thanks: { speaker: "Baker", text: "Bless you.", next: "check" },
check: { branch: [{ if: (v) => v.metMayor, next: "mayor" }, { next: null }] }, // invisible branch node
mayor: { speaker: "Baker", text: "Tell the mayor I said hi." },
bread: { speaker: "Baker", text: "Fresh from the oven." },
};
console.assert(validateDialogue(BAKER).length === 0, validateDialogue(BAKER)); // run in tests
const talk = runDialogue(BAKER, "start", game.vars, { onEvent: (e) => game.events.emit(e) });
// UI: talk.current → { speaker, text, choices: [{ index, text }] }; talk.choose(i) or talk.next(); talk.done
Data and tools
- Keep dialogue in data files (
data/dialogue/baker.jsor JSON with condition names instead of functions). - Variables (
game.vars) are part of the save. Flags for story state (metMayor), numbers for affection and money. - Writers can also use Ink or Yarn Spinner (both have JS runtimes) for big stories; this module is for small to medium games.
- Validate in tests: no missing nodes, no unreachable nodes, every choice list has an unconditional option.
Dialogue box UI
- Bottom of screen, speaker name + portrait, 2–3 lines max, typewriter at ~40 chars/s. Tap / click / Space skips the reveal, then advances.
- Choices as big buttons (touch-friendly), keyboard 1–4 or arrows + Enter, gamepad d-pad + A.
- A small arrow shows “more”. Allow a log/backlog (
talk.history) for story-heavy games. - Pause gameplay during conversations (push a dialogue scene: game-state-management).
Writing
Short lines, one idea each, the character’s voice. Choices should differ in meaning, not just wording. Show consequences
(a reaction, a changed variable). Let players skip text they’ve seen (seen).