Routes & flowchart
A visual novel without maps: the story is a list of events in order, split into routes. Each route gets its own page in the flowchart, where the player can load the story from any scene they have reached.
Visual novel mode
One switch in src/content/config/mode.ts. Only you can change it: players and the debug menu can't.
export default defineMode({
vnOnly: true, // false = the normal game with maps (everything as before)
})
With vnOnly: true:
- A new game starts the
commonroute instead ofnewGameinsrc/content/save. - The title screen and the pause menu have Flowchart (the title one is in
src/content/start.ts); the pause menu has no Inventory, Quests, Crafting, Travel, Party, Journal or "I'm stuck". The HUD and its keys only work on a map, so they are gone too. - The title screen has After Stories once one is open (below), and no New Game+.
- Extras has no Achievements tab. Achievements don't count towards "Found N%", and on their own they don't make Extras appear in the title menu.
- Saves remember where the story is in its routes, also in the middle of a scene.
The RPG code is still there, just not offered. game.vnOnly tells content which mode it is in.
A route
One folder per route: src/content/routes/<name>/index.ts. common always exists: every new game starts there.
import { defineRoute } from '@/core/define'
export default defineRoute({
title: 'Common', // tab and the box leading here
description: 'Arriving in town.', // optional
color: '#7fb3ff', // optional: tab and boxes leading here
// image: picture, // optional: import picture from './art.png'
run: async ({ event, choice, flag, check, startRoute, movie }) => {
await event('meeting/special', 'meeting')
await event('town/thechoice', 'the_choice')
// The player picks; two ways that join again below.
const trust = await choice({ yes: 'I trust you', no: 'Not yet' }, { who: 'jack', text: 'Well?' })
if (trust === 'yes') await event('jack/tells_his_story', 'tells_story')
else await event('jack/lies_about_himself', 'lies')
// Only sometimes: otherwise the line goes straight past it.
if (flag('met_anna')) await event('anna/greeting', 'greeting')
await event('town/leaving', 'leaving')
if (check('rich', (game) => game.money.get() > 100)) {
await movie('credits')
return // the story ends here: back to the title
}
if (trust === 'yes') return startRoute('anna')
return startRoute('bad_end')
},
})
event(id, key)plays an event and is one box of the flowchart. The key names the box: lowercase letters, digits and_, unique in the route. The same event may come twice with two keys. Events it runs inside itself belong to its box. Leave the key out and the box is named after the last part of the event id:event('common/day1_morning')is the boxday1_morning(in a .vn route:event common/day1_morning, noas).- Choices belong to the route, not to its events:
const pick = await choice({ stay: 'Stay in town', go: 'Take the bus' }), thenif (pick === 'stay'). The choice is a box of its own on the flowchart (look: theflowChoicepart; it shows the question, or "Choice"; the side panel lists the options), so the player can load the game right at it. Every option is a line leaving it, with the option's text on it. The box is named after the option keys (?stay_go); two choices with the same options on one way need a name:choice(options, { key: 'second_bus' }).choice(options, { who: 'jack', text: 'Well?' })shows a line above the options (nowho= narration). The choice shows over the stage the last event left (see the stage carries over); how it looks is the eventsrc/content/events/extras/choice. A save made while a route choice waits loads back to that choice, on the same stage. - Branch only with
choice(),flag('id')(yes/no flags fromsrc/content/flags.ts, e.g. set by an event) orcheck('label', (game) => …). That is how the flowchart sees both ways, and how saves and jumps take the same way again.rungets nogameon purpose. chapter('evening')starts a chapter (game.chapters.reach) between two boxes. The flowchart draws a line when the chapter NUMBER changes (see Chapters on the flowchart). Loading a save or a flowchart jump sets it again, without the toast.- What an option does by itself (points, a flag, an item):
choice({ wave: 'Wave back', ignore: 'Look away' }, { effects: { wave: (game) => game.relationships.change('anna', 1) } }). It runs when the player picks that option, and again, without toasts or sounds, when a flowchart jump passes this choice with that pick: you write nosetsfor it. Loading a save never runs it (the save already holds it). This is how a route is decided by points: give them on the choices, then branch withcheck('anna_likes_you', (game) => game.relationships.get('anna') >= 3). - Hidden options:
choice({ trust: 'I trust you', know: 'I know who you are...' }, { hidden: { know: { requiresEnding: ['anna'] } } }). A hidden option shows only when its conditions are met:requiresEnding= every ending in the list (src/content/endings/<id>) was reached in any playthrough on this device;requiredFlag= that flag is on; both = both. When only one option is left it plays by itself (no question; it counts as the player's pick, so its flowchart line is solid). Keep one option always shown: with every option hidden the first one is taken (a DEV warning says so). On the flowchart a hidden option's line (and its entry in the side panel) only shows once it can show or was picked. return ending('normal')ends the story there (back to the title) with that ending:src/content/endings/normalcounts as reached for good, shows in Extras → Endings (see events: the same endings an event reaches withending('id')) and can open hidden options (requiresEnding). A route can have several endings, one per way. A route that just ends (a plainreturn, or the end ofrun) goes back to the title without an ending. LikestartRoute, it is the last thing in its branch.startRoute('anna')moves on to another route (its own flowchart page). It is always the last thing in its branch: anything after it is an error. A route can't lead back to one it came from (common → anna → common is an error).- A route that ends without
startRouteends the game: back to the title screen. movie(id)plays a movie between two boxes (throughevents/extras/movie; it unlocks in Extras).
Events can still do everything they normally do, also change the flags the route branches on later.
The stage carries over
While a route plays, an event that ends leaves the stage as it is: the background (or CG), the characters where they stand, pictures from image(), weather, the filter, and a fade() colour. The next event (or a route choice) starts on it, with no black frame between them. Change or clear it in the script that should: bg none, hide, image none, weather none, filter none, fade none. Lines, choices, full-page text and the camera always reset.
- Saves keep the stage: a save made between two events, at a route choice, or in the middle of a scene loads with the same background and characters.
- Flowchart jumps start the box on the stage the box before it ended on, the last time that one played on this device (the game remembers it per box). A box whose earlier boxes never played here starts on an empty stage: give it its own
bgline if that matters. - A background chosen with
bgorcgis saved by its id, so a save still finds the picture after an update; one chosen by a file path is saved by that path. - Only in a route: in the normal game every event starts on an empty stage over the map, as before.
How the chart is made
Nobody draws the chart. The engine runs run without playing anything, once for every answer of every flag() / check(), and writes down the boxes. Boxes are the keys, so where two ways reach the same key, the lines join. The example above gives:
meeting → the_choice → tells_story ┐
→ lies ───────┼→ greeting → leaving → …
└──────────→ leaving
It shows every way the code allows. A way the story can never take (an event always changes a flag, say) still shows as "???" boxes: hide them with hidden in flow.ts. More than 4096 ways in one route gives a warning: split it into routes.
Mistakes in a route file (a key used for two events, something after startRoute, a loop) are errors with the route's name. pnpm test checks every route (src/content/routes.test.ts).
flow.ts: titles, descriptions, jumps
Optional, next to index.ts. Box names autocomplete.
import { defineFlow } from '@/core/define'
export default defineFlow('common', {
the_choice: {
title: 'The choice', // default: the event's title
description: 'Jack asks you to trust him.', // the side panel, before loading
// image: picture, // in the box and the side panel
},
tells_story: {
// What this event LEAVES BEHIND: runs instead of it for boxes on the way to a loaded box.
sets: (game) => {
game.items.add('letter')
game.relationships.change('jack', 5)
},
},
leaving: {
// What this event EXPECTS to find: runs just before it, when the player loads this box.
before: (game) => game.audio.music('music/town'),
},
impossible_box: { hidden: true }, // a way the story can't take
anna_greeting: { shift: { x: 40, y: -10 } }, // move the box (px); its lines follow
})
shift (and more layout fixes: Fixing the chart by hand) moves one box from where the chart puts it: x (+ right) and y (+ down) in px, both optional. It comes after the automatic layout, so only that box moves; its lines follow, and the other boxes stay where they were. Without any shift the chart is laid out exactly as before. Moving a box above the top moves the whole chart down.
What loading a box does
Loading leaving through tells_story:
- A fresh game (like New game; the difficulty is kept, nothing from New Game+ is carried).
- Every box on the way runs its
setsinstead of playing. The way'schoice()picks are replayed, and flags the route asked withflag()are set to the way's answers by themselves (a way throughif (flag('met_anna'))setsmet_anna = true): only write what the chart can't know (items, points, money, numbers,check()conditions). - The loaded box runs
before, then its event plays, and the story goes on normally.
.vn scenes need no sets for their plain lines. The engine reads what a .vn scene leaves behind from its set, love, money, give, take, xp, join, achievement, codex, quest, rename and reveal lines, in order and inside the ifs they are in (an if on flag(), has(), love(), money(), seen() or game.…), and runs that on a jump. A sets in flow.ts replaces it for that box. It can't know what a choice inside the scene picked, an if on a value of the scene ($ const roll = random()), a $ line that changes the game, or a nested event: then the parts it knows still run, and in development builds a jump through that box warns you to write a sets for it. Choices are best kept in the route anyway, with what an option does by itself (see above).
Skipped events count as played (game.events.seen). Nothing is shown to the player while sets runs (no toasts, no sounds).
Keeping sets honest: in development builds, each time a box really plays, the engine compares what its event changed with what its sets (or what the engine read from its .vn lines) would do, and warns in the console when they differ ("its flow.ts sets does not leave what the event left: data.flags.met_anna").
Which way loads (VN style)
- A box can be loaded once the player has reached it in any playthrough, through any way made of boxes they have reached, even if that exact run never happened. Played
lies → leavingonce andtells_storylater? Thenleavingalso loads throughtells_story. - Where ways join, the box the player picked most recently in the chart wins; then the one played most recently. The way that would load is lit up before the player loads.
- A box in a later route loads through the earlier routes' boxes up to the box leading into it.
- Never through a dotted line. A way only counts if every choice on it uses an option the player picked themselves (a solid line). A box reached some other way (a save, a replay) whose every way goes through an option never picked can't be loaded: the side panel says so (
text.noWayinsrc/content/config/flowchart.ts) until the player picks that option in the story. The choice box itself can still be loaded (it asks again).
The flowchart window
Side panel (picture, title, route, description, Load), the chart, and a tab per route reached. The window starts on the box the story is at.
- Keys / pad: arrows move between boxes (the panel follows), confirm picks a box (its way lights up), confirm again asks to load it. LB / RB (Q / R) switch routes.
- Touch / mouse: tap a box, tap it again (or press Load).
- Boxes not reached yet are invisible (VN style: they keep their place, so the chart doesn't jump around, but nothing of the future shows, not even how many endings there are). Every line leaving a reached box is drawn (dashed into a box not reached yet), but nothing past that box: the player sees that a scene branches, not where to.
unseen: 'locked'insrc/content/config/flowchart.tsshows them as "???" instead. - A choice option the player never picked stays dashed, even when its line ends at a box they reached another way. Picks are remembered per device, like reached boxes (picks replayed by a save or a jump don't count).
- Diamonds: every
flag()/check()that splits the way between two boxes is a small diamond (not a choice: the way depends on a flag or an earlier choice). The "no" way runs straight down through it; the "yes" way leaves from its side (branch.side, default right) and turns down into its own box. Diamonds can't be picked or loaded; one shows once the box before it is reached. A flag that leads to the same box either way gets no diamond.branch: null= no diamonds (lines split at the box, yes on the left). - A line picked with a choice carries the option's text (
choiceLabelinsrc/content/config/flowchart.ts;null= none). - A box leading into another route opens that route's tab.
Make it look like your game:
- Box looks: the
flow…parts insrc/content/look/parts.ts:flowNode(every box, withfocus), and added on topflowNodeLocked,flowNodeSelected,flowNodeOnPath,flowNodeCurrent,flowRouteLink,flowEnd,flowChoice;flowBranch(the diamonds) andflowBranchOnPath;flowPanel;flowTab(withselected);flowTabHeading(the "Routes" heading),flowTabDivider(the line and heading over the after stories' tabs). A box can add its own part:look: 'myPart'in flow.ts. - Lines, sizes, wording:
src/content/config/flowchart.ts. Boxes sit in the middle of their branches, and a box where ways join again goes back under the box where they split, so a long chart stays centered instead of drifting to one side. Several lines leaving or entering one box use their own points along its edge, so they never run on top of each other. When several lines come into one box, the ones coming from far off to a side go on past its top and enter that side. A diamond's lines leave from its side corners when they go off to a side; when two corner lines in one row would overlap, the later one turns down early and enters its box from the side. Where a line runs across another, it hops over it with a small arc (hops), so a crossing never looks like a join. - The window itself:
src/content/windows/flowchart. It only draws:useFlowchart()(from@/core/routes/useFlowchart) gives the boxes, lines, tabs, selection andload(). One file per part:index.tsx(the window and its layout),Boxes.tsx(boxes and diamonds),Lines.tsx+linePath.ts(lines, their shapes and hops),ChapterLines.tsx,SidePanel.tsx,RouteTabs.tsx,DebugGrid.tsx.
export default defineFlowchart({
node: { width: 168, height: 52, gapX: 36, gapY: 44, image: 'left' }, // box picture: 'left' | 'right' | 'top' | 'bottom' | 'none'
shape: 'rounded', // right angles with round corners | 'elbow' | 'curve' | 'straight'
corner: 10, // corner radius of 'rounded' lines, px
hops: 5, // a small arc where a line runs across another, radius px ('rounded' / 'elbow'; null = off)
branch: { size: 26, side: 'right' }, // flag()/check() diamonds; null = none
arrows: false, // true = a small arrow where a line meets the box below
chapters: { header: 30, size: 12, color: gold, line: { color: '…', width: 1, dash: [6, 4] } }, // null = none
lineGap: 16, // least px between side-by-side lines
autoFix: true, // lines running across between two rows never lie on top of each other
compact: null, // px gap between rows whose lines all go straight down (null = gapY)
labels: 'middle', // where option text sits: 'start' | 'middle' | 'end'
debug: false, // dev builds: rows, columns, px and every box / diamond key
line: { color: 'rgba(255, 255, 255, 0.35)', width: 2 },
pathLine: { color: gold, width: 3 }, // the way that would load
lockedLine: { color: 'rgba(255, 255, 255, 0.2)', width: 2, dash: [5, 5] }, // 'locked' style only
unseen: 'hidden', // boxes not reached: 'hidden' (no spoilers) | 'locked' ("???")
choiceLabel: { color: gold, size: 12, outline: 'rgba(10, 12, 18, 0.9)' }, // null = no labels
text: { locked: tx('???'), end: tx('The end'), load: tx('Load'), loadQuestion: tx('…'), cannotLoad: tx('…'),
routes: tx('Routes'), // heading over the main story's tabs (null = none)
afterStories: tx('After Stories') }, // heading over the after stories' tabs (null = line only)
panel: { width: 0.32, side: 'left' },
tabs: { side: 'right', image: 'top' }, // tab picture: 'left' | 'right' | 'top' | 'bottom' | 'none'
height: 0.72, // share of the screen's height
})
Chapters on the flowchart
A chapter('id') in a route draws "Chapter 1" over a line across the whole chart. There is one line per chapter NUMBER: sub chapters never add one (1.1 and 1.2 are both under "Chapter 1"; a route whose first chapter is 1.1 still gets the heading "Chapter 1"; "Chapter 2" only comes with the first chapter numbered 2). Sub chapters show on saves ("Chapter 1.2: …"). Every box played after the first chapter of a number is below its line; every box before it is above. The same chapter may be started in several places (in different branches): it is still one line. A branch that starts it later than the others keeps its extra boxes above the line, even when they play at the same step as boxes the other branches show below it:
chapter('one')
await event('x/a', 'a')
const p = await choice({ x: 'X', y: 'Y', z: 'Z' })
await event(`x/${p}1`, `${p}1`)
if (p !== 'z') chapter('two') // x and y: chapter 2 starts before their 2nd event (number: 2)
await event(`x/${p}2`, `${p}2`) // z2 stays above the line (still chapter 1); x2, y2 below it
if (p === 'z') chapter('two') // z: only now
await event(`x/${p}3`, `${p}3`) // all below the line
await event('x/m', 'm') // the branches join
A box reached by ways in different chapters goes with the latest one. A chapter before the route's first box puts its line at the very top. In the hidden style a chapter's line shows once one of its boxes is reached. Looks: chapters in src/content/config/flowchart.ts (header = room for the number above the line, px; text size / color; line; null = no chapter lines, boxes placed as if there were none).
Fixing the chart by hand
If the automatic layout puts a box, a diamond or a line somewhere you don't like, fix it in the route's flow.ts. Every option is optional: a box without them stays where the chart puts it, and a route without any is laid out exactly as before. They work on event boxes, choice boxes ('?stay_go': '?' + the option keys, or + the choice's key) and flag diamonds ('!leaving' = the diamond right after the box leaving; '!leaving:false' / '!leaving:true' = one further on, after a "no" / "yes"). Set debug: true in src/content/config/flowchart.ts (dev builds only) to see every key, the row and column numbers and px.
export default defineFlow('common', {
waved: {
shift: { x: 40, y: -10 }, // move it, px (+ right / down); its lines follow, nothing else moves
column: 2, // or: put it in debug-grid column 2 (0 = the first box; may be negative)
row: 5, // put it in row 5 or lower (never above what leads to it); later boxes move down too
order: 0, // its place in its row, 0 = leftmost (the others keep theirs)
align: 'arrival', // same column as another box
},
jack_story: {
branchSide: 'left', // "yes" leaves its diamond to the left
branchShift: { x: 12 }, // move the diamonds right after this box
lines: { // its lines (and its diamonds' lines), by the box they go to
anna_greeting: {
straight: true, // this one goes straight through the diamond; "no" leaves from the side
bend: -8, // move the part that runs across up (-) / down (+), px
start: -20, end: 10, // where it leaves / enters: px from the box's middle
enter: 'left', // into the box's 'top', or past the top into its 'left' / 'right' side
look: { color: '#ff8a80', dash: [2, 4] }, // its own colour / width / dashes
},
leaving: { via: [[300, 620]] }, // go through these chart points (px, see debug) instead
},
},
'?stay_go': { // a choice box
lines: {
'@route:bad_end': { label: 'Leave', labelShift: { y: 6 } }, // own text (null = none), moved
'@route:anna': { hide: true }, // don't draw it
},
},
'!jack_lies': { shift: { y: 6 }, branchSide: 'left' }, // a diamond itself
})
Settings on a box apply to the lines leaving its diamonds too, by the box they end at; to reach a line between two diamonds, write it on the first diamond ('!a': { lines: { '!a:false': … } }). '@end' and '@route:<id>' are the end and route boxes. Strongest first: column, then align, then straight lines; shift is applied last. row / column / align make room by moving other boxes; shift and via don't (you choose the place, so nothing checks for overlaps).
After stories
Extra stories played after an ending, started from the title screen. VN mode only. An after story is a normal route with one more field:
// src/content/routes/anna_after/index.ts
export default defineRoute({
title: 'A year at the pond', // the card's title (and the flowchart tab)
description: 'Anna, one year after you stayed.',
image: card, // the card's picture
color: '#7fb3ff',
afterStory: { requiresEnding: ['anna'] }, // ALL listed endings reached ([] = open from the start)
// afterStory: { requiresEnding: ['anna'], order: 1 } // lower comes first; default folder order
run: async ({ event }) => {
await event('anna_after/morning')
},
})
In a .vn route it is one header line: after story [ending anna] (more [ending …] = all of them).
- The title button shows only while at least one after story is open. Reaching an ending that no after story needs (a bad end) adds nothing. The menu entry is in
src/content/start.ts. - The selection screen is a full-screen minigame,
src/content/minigames/after_stories: change it or replace it. Its params are{ available, locked, launch }(fromgame.routes.afterStories()). Each entry has the route'sid,title,description,image,color,requiresEnding, andplayed. Callparams.launch(id)to start one (it also ends the minigame), orfinish(null)to go back. The demo draws locked ones as a black silhouette of their picture (use pictures with a see-through background) that can't be picked. - It starts from a fresh game (the difficulty is kept, nothing else). Its first event sets what it needs (
love anna +10,set …). The way it plays, saves, choices, chapters andending()all work as in any route. - Flowchart: after stories, and the routes they move on to with
startRoute(), get their own group of tabs under a line (heading:text.afterStoriesinsrc/content/config/flowchart.ts,null= the line only; the main story's tabs havetext.routesover them; look: theflowTabDividerpart). Each group scrolls by itself. Loading a box there starts from the after story, not fromcommon. - A minigame started from the title screen is drawn over it.
Routes as .vn scripts
routes/<name>/index.vn works instead of index.ts (see routes/bad_end). flow.ts stays TypeScript.
# The common route
title Common
description Arriving in town.
color #7fb3ff
event meeting/special as meeting
event town/thechoice as the_choice
choice jack: Well?
* I trust you
# this event sets the flag jack_trusted (routes read flags; events set them)
event jack/tells_his_story as tells_story
* Not yet
event jack/lies_about_himself as lies
if flag('met_anna'):
event anna/greeting as greeting
chapter evening
event town/leaving as leaving
if check('rich', game.money.get() > 100):
movie credits
end
elif flag('jack_trusted') and not flag('met_anna'):
route anna
route bad_end
Right under an option you can write what picking it does: love anna +1, set waved = true, money -5, give apple, take apple (also xp, join, achievement, codex, quest, rename, reveal), before the option's other lines. They become the choice's effects:
choice:
* Wave back
love anna +1
event town/waved
* Look away
event town/ignored
A choice works like in an event script (each option's text is also its key). choice: alone has no question line; choice: text asks it as narration.
Lines: title (required), description, color, image ./file.png, after story [ending <id>], event <id> as <box>, choice with * option lines, if / elif / else:, movie <id>, chapter <id>, route <id>, ending <id> (= return ending('id')), end (ends without an ending). A hidden option ends with its conditions: * I know who you are [ending anna] (several [ending …] = all of them), [flag met_anna] (both kinds may be given). Conditions: flag('id') and check('label', condition) with and / or / not. The editor extension checks and completes event, route, chapter, ending and flag ids.
Opening the game at a scene while you work: @start
Put @start on a line of a scene (an event a route box plays). While the dev server runs, every page load skips the boot screens and the title, loads that box like a flowchart jump, and runs the scene instantly up to the marker: the line after it is the first one you see. Save the file, the page reloads, you are there again.
# src/content/events/vn_demo/jack_story/index.vn
jack: Good. Then I'll tell you everything.
jack: I came here ten years ago, with nothing but a letter.
@start wave trust
jack: Nobody asked where I was from. # the game opens here
// a .ts scene
import { devStart } from '@/core/dev/start'
await say('jack', 'I came here ten years ago…')
devStart({ way: ['wave', 'trust'] })
- The way: where several ways lead to the box, name the route's answers on the way, in the order they are asked (from the common route on): a choice by its option key (in a .vn route, the option text:
"I trust you"), aflag()/check()byyes/no. Without them it never guesses: the console and a toast list each way with the marker that names it, and the game starts normally. Where only one way leads there, a plain@startis enough. - The box: when the scene plays in more than one box, add it:
@start box common/jack_story wave trust(in .ts:devStart({ box: 'common/jack_story', way: […] })). - Before the marker everything happens at once, like loading a save: lines go by (not counted as read), a choice inside the scene takes its default answer, text input its default. Back still goes into those lines.
- Routes (visual novel mode,
vnOnly: true) only. One marker at a time (the first found counts; the others get a warning). - Never shipped:
pnpm build:web(and every other build) fails while a marker is left in, naming its file and line. Outside the dev serverdevStart()does nothing.
game.routes
| Call | What it does |
|---|---|
game.routes.start(id) | Starts a route from its top (a new game does start('common') in VN mode). |
game.routes.current() | { route, key } of the box the story is at, or null. |
game.routes.seen(route, key) | Reached in any playthrough? |
game.routes.jump(route, key) | What the flowchart's Load does. Resolves false if the box can't be loaded. |
game.routes.way(route, key) | The way that would load (boxes and answers, per route). |
await game.routes.afterStories() | { available, locked, launch(id) }: the after stories, in order (empty outside VN mode). |
await game.routes.startAfterStory(id) | Starts an open after story from a fresh game. False if it isn't one, or isn't open. |
game.routes.graph(id) | A route's chart: boxes, lines (with the branch each was taken on), every way. |
Seen and picked boxes are kept per device across all saves (like read lines), and are part of backups.