Interactive fiction and choice-driven romance stories rely on a surprisingly compact core state machine: scenes, conditional choices, relationship/trust meters, and a turn-stamped memory ledger so later chapters remember player decisions.
Below is the minimal architecture behind branching-story-kit (MIT) — used in our playable browser short The Midnight Bookshop and inspired by our work on Ouba.
Core State Engine (index.mjs)
export class StoryEngine { constructor(story, initialState = {}) { this.story = story; this.sceneId = story.start; this.state = { ...(story.initialState || {}), ...initialState }; this.history = [story.start]; this.memory = []; } current() { const scene = this.story.scenes[this.sceneId]; const availableChoices = (scene.choices || []).filter((c) => !c.requires || Object.entries(c.requires).every(([k, minVal]) => (this.state[k] ?? 0) >= minVal) ); return { id: this.sceneId, title: scene.title, body: scene.body, choices: availableChoices, state: { ...this.state } }; } remember(fact) { this.memory.push({ turn: this.history.length, fact: String(fact).trim() }); return this.memory; } choose(index) { const choice = this.current().choices[index]; for (const [k, delta] of Object.entries(choice.effects || {})) { this.state[k] = typeof delta === "number" ? (Number(this.state[k]) || 0) + delta : delta; } this.sceneId = choice.next; this.history.push(this.sceneId); return this.current(); } } Try It & Join Our Monthly Writing Jam
- Playable Browser Demo: https://huggingface.co/spaces/oubaapp/the-midnight-bookshop (mirror: https://ouba-midnight-bookshop.vercel.app/)
- Free Monthly Interactive Fiction Writing Jam (Luma): https://luma.com/evycmvzq
- Explore Full Branching Stories on Ouba: https://www.ouba.art/
- Companion Trope & IF Benchmarks: https://ficfare.com/ · https://slowburnlibrary.com/
