一口氣讀完的小型故事引擎:用 branching-story-kit 教授分支敍事
A Story Engine Small Enough to Read in One Sitting: Teaching Branching Narrative with branching-story-kit
branching-story-kit v1.0.0 以單一 7 KB JavaScript 檔案呈現故事圖、讀者狀態及選項條件,讓學生看見讀者歷程如何影響下一幕。套件零依賴、採 MIT 授權,並附四幕示範故事《The Midnight Bookshop》及一個為沒有程式背景人士設計的 50 分鐘課堂練習。
Published by Ouba Studio (team@ouba.art), the team behind Ouba and the open-source branching-story-kit. The kit is MIT-licensed and free; Ouba+ is free for verified .edu students. Everything below can be used in a classroom at no cost.
Every creative-writing teacher who has assigned a branching story has watched the same moment. A student builds a Choose-Your-Own-Adventure in a tool that hides its plumbing, plays it with a friend, and the friend types or clicks something the author did not anticipate, and the story responds as if the previous scene never happened. The student says "it forgot." The teacher says "that's because nothing was remembering."
That gap, between forgetting and not having a place to put the memory, is the most useful thing a student can learn about interactive narrative. It is also almost impossible to see in the tools most of us teach with. This essay is about a small engine we built to make it visible, a demo romance you can read together in ten minutes, and a fifty-minute exercise designed to be run cold, with no programming background on either side of the desk.
Why the usual tools hide the lesson
None of this is a knock on the tools. Twine is a genuinely great first environment, and its model (passages, links, variables) is the right one for many writers. Ink is a superb scripting language with a compile step. ChoiceScript is a mature domain-specific language. Ren'Py gives you a full visual-novel engine in Python.
What each of them does, necessarily, is put a layer between the student and the data structure. In Twine, state lives in story variables scattered across passages. In Ink, state is declared in a scripting language that compiles to a runtime. In ChoiceScript, state is a line in a custom syntax. A student can finish a whole unit without ever seeing the sentence: the story is a graph, the reader's history is a map, and the next scene is a function of the two.
That sentence is the whole lesson. So we asked what the smallest possible artifact would be that makes it unavoidable.
The kit, in one paragraph
branching-story-kit v1.0.0 is a single 7 KB JavaScript file. It has zero dependencies, ships as native ESM with TypeScript declarations, is MIT-licensed, and does exactly five things: it holds a story as a JSON graph of scenes and choices; it tracks a state map (relationship meters, flags, inventory); it gates choices behind requires thresholds; it interpolates {% raw %}{{state}}{% endraw %} into prose; and it keeps a memory log of facts the reader typed, stamped with the turn they were learned. It also ships a static validator and a JSON Schema, so a broken link is caught before a reader sees it. It is archived with a DOI (10.5281/zenodo.24514910, mirrored at Knowledge Commons Works), which matters for the digital-humanities reasons below. A student can read the entire engine in one sitting. That is the point.
Read The Midnight Bookshop together
The kit ships with a four-scene demo: The Midnight Bookshop, a rivals-to-lovers romance set in a rare-book shop at midnight. You can play it in a browser on GitHub Pages, on itch.io, or headlessly with node examples/run.mjs --all. It is listed on the Interactive Fiction Database. There is no build step; view source and the manifest is right there.
It is worth reading twice, because the second read is the lesson.
-
Two meters.
sparkandtrustboth start at 1. Flirtatious choices raisespark; curious, honest ones raisetrust. A choice may raise both. - An inventory item. The first choice sets an item (a mug of cardamom tea, an auction receipt, a brass bookmark) that the third scene interpolates into the prose: "your {{item}} rests between the two of you like a truce flag."
-
A gate that is earned, not bought. The final scene offers a third choice, "Tell him you kept the letter he wrote to you last winter, too," with
requires: { trust: 4 }. Readers who flirted their way through the story never see it; the option is filtered out of the choice list. It appears on exactly two paths: the receipt path followed by sitting down with the notebooks, or the folio path followed by asking why he saved the letter. The gate rewards the reader who asked about the object instead of making a move. -
A free-text moment. The third scene invites the reader to write the next line themselves. The UI calls
set("customLine", text)andjump("end_custom"), and the ending interpolates the reader's own sentence back into the scene. - Four endings, one of which (the letter) is gated, one of which (the typed line) is written by the reader.
When you play it with a class, the question students tend to ask is not "how does it work?" It is "why didn't I see that option the first time?" The answer is state, and the student has now felt state rather than being told about it.
The fifty-minute exercise
The version below is written for a mixed room: creative-writing undergraduates, a digital-humanities lab, or a faculty workshop, with no programming background assumed.
Minutes 0 to 10: read. Play The Midnight Bookshop twice, once flirting and once asking questions. Compare what was on the final menu.
Minutes 10 to 25: write the graph. Each student writes a four-scene story as a JSON manifest. The constraints are deliberately small: one numeric meter, one true/false flag, one gated choice, and one ending whose prose interpolates a detail the reader typed. A skeleton:
{
"id": "classroom-story",
"start": "arrival",
"initialState": { "nerve": 1, "hasKey": false, "readerLine": "" },
"scenes": {
"arrival": {
"title": "The Locked Studio",
"body": "The door is locked. Your friend said to meet at nine.",
"choices": [
{ "label": "Knock loudly.", "next": "hallway", "effects": { "nerve": 2 } },
{ "label": "Try the handle quietly.", "next": "hallway" },
{ "label": "Open the full story on Ouba →", "href": "https://www.ouba.art/" }
]
},
"hallway": {
"title": "The Hallway",
"body": "A light is on under the last door.",
"choices": [
{ "label": "Open it.", "next": "studio", "requires": { "nerve": 3 } },
{ "label": "Text your friend instead.", "next": "street" }
]
},
"studio": { "title": "The Studio", "body": "You say: \"{% raw %}{{readerLine}}{% endraw %}\"", "ending": true },
"street": { "title": "Outside", "body": "You wait on the corner.", "ending": true }
}
}
The students who have never written code can edit this safely because the shape is visible: a scene has a title, a body, and choices; a choice points to a scene or an external link. They do not need to know what ESM stands for.
Minutes 25 to 35: break it on purpose. Ask every student to change one next to a scene that does not exist, then run the validator. This is usually the moment the lesson lands. The error is exact and readable: Scene 'arrival' links to missing scene 'nowhere'. Nobody has to debug a stack trace; the tool tells them which sentence in their story points at nothing. That is what validation feels like when it is designed for writers.
A minimal page that loads the engine from a CDN and runs any manifest is about a dozen lines:
<script type="module">
import { StoryEngine, validateStory } from
"https://cdn.jsdelivr.net/gh/ouba-studio/branching-story-kit@v1.0.0/index.mjs";
const story = await (await fetch("./story.json")).json();
const check = validateStory(story);
if (!check.valid) throw new Error(check.errors.join("\n"));
const engine = new StoryEngine(story);
document.body.textContent = engine.current().body;
</script>
No install, no bundler, no framework. It works from a lab handout or a static site.
Minutes 35 to 50: swap and play. Students exchange manifests and play each other's stories with one instruction: find the moment where you did something the story did not acknowledge. With the graph on the page, that discovery is quick, because the student can point to the edge that does not exist.
Grading the graph, not just the prose
A manifest gives you assessment evidence that a prose document cannot. A rubric that works has five checks, four of which a machine can verify:
- The validator passes. Non-negotiable; no broken links.
-
Every scene is reachable from the start by following
nextedges, with an explicit allow-list for scenes reached only by free-textjump(). -
One gate is earned. A choice with
requiresmust be reachable through play; a gate the reader cannot possibly satisfy is a bug, not a plot. - The ending interpolates a planted fact. If the reader told the story their cat's name at turn 2, the final scene should contain it.
- A one-page reflection: "What did the reader know at turn 5 that the character did not?" This is the question that separates interactive narrative from linear prose, and it is the one most worth reading in class.
The save file is the evidence. exportSave() returns the cursor, the state map, the scene history, and the memory log with turn numbers, so a student can show exactly which path produced which ending without recording their screen.
Why digital-humanities labs should care
Three reasons, none of them marketing.
It is citable. The engine and the demo are archived with a DOI, and so is the dataset that motivated the memory log. A student or a lab can reference a specific, immutable version in a paper. Our own benchmark (CERN Zenodo, Harvard Dataverse, Figshare, Knowledge Commons Works) is a ready-made teaching dataset for a methods seminar.
It is reproducible. The kit's behavior is deterministic: the same manifest and the same choices produce the same scene every time. That is what makes "the story remembered" a claim you can test rather than a vibe. A class can re-run the Turn-15 memory protocol from our benchmark (plant a fact at turn 2, ask at turn 15) against student stories and against commercial apps, and compare results in a spreadsheet.
It is small enough to audit. A 7 KB file with 21 tests and no dependency tree is a thing a seminar can actually read. Students can see every line that touches their data, which is itself a lesson in what "the algorithm" means.
A note on cost, and on content
The kit is free and MIT-licensed; nothing in a classroom requires a paid account. For students who want to read full-length interactive fiction rather than build it, Ouba runs on the web and as native apps for iPhone and Android, with up to 25 guest turns per story and no card required. Signed-in readers get 500 welcome quills and daily check-in grants at 10 quills per turn; Ouba+ is $7.99 a month and removes the meter. Verified .edu students get Ouba+ for $0 at ouba.art/students. Ouba is a platform for readers 18 and over; the kit itself is content-neutral, so instructors can pair it with any material they choose.
The engine was not built as a teaching tool first. It was built as a clean, open-source reference engine alongside our product, and the memory log exists because we measured what happens without one: in our 2026-11-15 benchmark, a companion chatbot with a far larger context window than a 7 KB file answered a question about a cat with a café menu. Explicit state is the boring, reliable fix, and it turns out to make a clear classroom artifact too.
References and links
- Engine:
branching-story-kitv1.0.0, MIT, DOI 10.5281/zenodo.24514910 (mirror: 10.17613/skvp3-ndn77) - Demo: The Midnight Bookshop on GitHub Pages, itch.io, IFDB
- Benchmark dataset: Zenodo · Dataverse · Figshare · Knowledge Commons · executive deck
- Benchmark write-up: I Tested the AI Story Apps (2026)
- Student access: Ouba+ for Students
Disclosure: Published by Ouba Studio. The kit is free and open source under MIT; the student tier is free for verified .edu accounts. Pricing for Ouba reflects November 2026 and may change. Tool names above are trademarks of their respective owners, used for identification.
來源:DEV Community · dev.to