Skip to content

Live refresh and debug

One small localhost link between Patterpad and your running game buys you two things.

Live refresh means you save in the editor and the running game picks up the edit without restarting. Reword a line and the game speaks the new words the next time it comes up; even restructured scenes carry the run across.

Live debug means the game streams its story cursor back, and Patterpad follows it like a debugger. The current beat highlights, scenes switch as play crosses them, and you can see which flow is where.

The debug half is observe-only, so the game stays in control and the editor is a passive mirror. The link is a loopback-only WebSocket (127.0.0.1), so only processes on your own machine can reach it, and nothing leaves your machine. Web pages are refused too, unless they are served from your own machine (localhost), so a site open in your browser cannot connect and read your story.

Every engine ships a client (JavaScript, Unity, Unreal, Godot), all speaking the same patterplay/debug@1 protocol below. Each is a debug-only tool, inert in a shipping build and safe to leave wired in (see the per-engine notes).

The link is controlled by a small connect icon in the bottom-right corner of the editor (and by the Play ▸ Live Link menu item, which is ticked while the link is on).

The Live Link chip in the editor's bottom-right corner: the Live Link label and the loopback address ws://127.0.0.1:4471 beside an amber connect icon, with a tooltip reading Live Link is listening for a game. Click to stop.
The connect icon, amber and listening. The loopback address ws://127.0.0.1:4471 sits beside it (click to copy); hovering spells out the state.
  1. Click the connect icon (or tick Play ▸ Live Link). It turns amber (listening) and the address (ws://127.0.0.1:4471) appears beside it. Click the address to copy it.
  2. Run your game with its link client pointed at that address (below). When it connects the icon turns green and the editor starts following the cursor.
  3. Click the icon again (or untick the menu item) to stop.

The icon’s colour is the state, and hovering it spells the status out:

  • Grey is off.
  • Amber is listening, waiting for a game.
  • Green is connected and in sync, so the game is running this exact build and beats highlight precisely.
  • Red is connected, but running a different build.

Red means you’ve rebuilt or edited since the game launched, so beat ids may not line up. The editor still follows scenes, but rebuild and relaunch to re-sync for exact-beat highlighting. A game wired for live bundle refresh re-syncs itself: saving in Patterpad pushes the new bundle straight into the running game, and a game that connects on an older build is sent the current one as soon as it says hello.

When the game goes away, the playhead it was moving clears. The game’s link reconnects by itself. Restart Patterpad, or turn the Live Link on after the game has started, and the game connects again within a few seconds: it tries after a second, then less often, up to every ten seconds, until it’s back. Patterpad takes one game at a time, so a second game that connects takes the link, and the first stops trying until you switch its link off and on.

A game can run several flows at once: a conversation, a bark, ambient chatter. If more than one flow is live, a small flow picker appears next to the address. It names the flow the editor is following, and clicking it lists every live flow with the scene each one is in.

  • Most recent is the default. The editor moves to whichever flow played last, so it keeps up with a game whose flows open and close all the time. With one flow, it follows that flow.
  • Pick a flow to stay on it. Lines from the other flows don’t move the editor. Choose Most recent to go back.
  • When the flow you’re following ends, the editor moves to the flow that played most recently and shows its current line straight away. A flow you picked counts as done when it ends, and following goes back to the most recent.

Flows are grouped by the part of their name before the first slash, so patterstage/gate/talk is listed as gate/talk under patterstage. Hover a flow for its full name.

When the game reports the option a player took, the editor moves the playhead onto that option’s prompt. As play moves on, the prompt keeps the small dot every line the game passes leaves behind, so you can see which way the conversation went.

With the link connected, Patterpad doesn’t just watch your game. Saving in the editor pushes the freshly compiled bundle into the running game, which picks it up without restarting. Reword a line, hit save, and the running game speaks the new words the next time that line comes up. For a writer, this closes the loop completely. Play your actual game, feel a line land wrong, fix it, and hear the fix on the next pass, with no rebuild, no restart, and no losing your place.

Two tiers, picked automatically:

  • Text-only edits swap the string tables in place. Patterplay restarts nothing and touches no state; under Patterstage, the line playing starts again with its new words.
  • Structural edits carry the whole run across (a save and load behind the scenes). Position is re-found by id, so lines inserted or reordered before the cursor neither replay nor shift where you are; an option you deleted drops out of an open choice; content deleted under the cursor is skipped and play continues from the nearest survivor.

JavaScript wiring, via applyLiveBundle (a one-time developer task; writers just save):

import { createDebugLink, applyLiveBundle } from "@patterkit/play-helpers";
let engine = new Engine(BUNDLE);
let bundle = BUNDLE;
let flow = engine.openFlow("main");
const link = createDebugLink({
build: engine.buildId,
onBundle: ({ build, data }) => {
const r = applyLiveBundle(engine, bundle, data); // picks the tier itself
engine = r.engine; bundle = r.bundle;
if (r.kind === "structure") flow = engine.getFlow("main"); // re-bind your flow handles
link.setBuild(build); // the editor's pill flips back to in-sync
},
});

Every engine receives the push. The native wiring mirrors the JavaScript shape, adapted to each engine’s threading.

In Unity, drain the link from your Update() (the socket runs on a worker thread), then apply if (_link.TryReceive(out var raw) && PatterLiveBundle.TryParsePush(raw, out var build, out var data)) { var r = PatterLiveBundle.ApplyLiveBundle(_engine, _bundle, data); … _link.SetBuild(build); }.

In Unreal, set Link->OnBundle, which fires on the game thread. Load with UPatterBundle::LoadFromString(Data), apply with Engine->ApplyLiveBundle(NewBundle) (the engine object and every UPatterFlow handle swap in place and stay valid), then Link->SetBuild(Build). A C++ host that plays the core patter::Engine directly calls patter::applyLiveBundle(Engine, Current, Next) (Patter/LiveBundle.h) instead: it picks the tier by the same rule and hands back the engine to keep using.

In Godot, connect the link’s bundle_pushed(build, data) signal. Apply with engine.apply_live_bundle(data) (re-bind flow handles on a "structure" result), then link.set_build(build).

The same swap powers Patterpad’s own Play window. Edit mid-run and it applies live (a quiet “Edits applied live” note), only falling back to the restart prompt when the in-flight edit doesn’t compile. The cross-bundle behaviour is locked by the shared conformance corpus, so all four engines resolve an edit under the cursor identically.

There are honest limits. Your game’s own side-effects don’t rewind (things already spawned stay spawned); text already in a transcript keeps the words the player saw; and an edit that changes how many random draws happen before the cursor naturally changes later draws.

A pushed bundle lives only in the running game. The next run, and your engine’s editor (its pickers and checks), still read the bundle you last published, so publish to keep the edit (Publish ▸ Publish Bundle, see Publishing). While the game is running edits you haven’t published, Unpublished edits shows beside the Live Link icon, and it clears once you publish.

With a game connected, a writer can jump straight into any conversation: right-click a block (its ⋯ menu) and pick Play in game, or put the caret in it and use Play ▸ Play Block in Game. Patterpad saves first, so the game has what the writer sees, then asks the running game to start that block now. Play in game, taking over asks the game to stop what’s playing first, so the block needn’t wait for a quiet moment. What that stops is the game’s to decide (Patterstage stops everything playing on that director). The game’s answer shows in a note: the block started, or why it didn’t.

What “start this block” means is the game’s to decide: which characters say the lines, whether the player takes part, what a busy speaker does. Patterplay hands the request over and carries the answer back. Patterstage does all of this for you, and asks as the game itself would. If one of the game’s triggers asks for that block, the request is made as the trigger has it (its requester, bindings, broadcast, and type); otherwise it has no requester, and the player’s speaker is bound to the player character only when the block has lines for them. So a once line is spent on the memory it would use in play: the trigger’s requester’s, or, with no trigger, that block’s own. A game of your own handles it like this, in JavaScript:

const link = createDebugLink({
build: engine.buildId,
onPlay: (req) => { // { id, sceneId, blockId, takeOver, build? }
if (req.build && req.build !== link.build) {
link.replyPlay(req.id, { started: false, reason: "staleBuild" });
return;
}
const started = startConversation(req.sceneId, req.blockId, req.takeOver); // your game's own
link.replyPlay(req.id, started
? { started: true }
: { started: false, reason: "busy", detail: "Mara is talking." });
},
});

Answer at once or a frame or more later, from the game thread; Patterpad waits a few seconds, and an answer after that is ignored. reason is a short code, and any code passes through: Patterpad has plain words for busy, unknownBlock (the game doesn’t have that block), staleBuild (the game is on an older bundle), and unhandled, and shows any other code as it is. Put the specifics in detail. The ids are the bundle’s opaque model ids, as everywhere on the link.

In Unity, the request arrives through the same TryReceive drain as a pushed bundle: if (PatterPlayInGame.TryParsePlay(raw, out var req)) { … _link.ReplyPlay(req.Id, new PatterPlayResult { Started = true }); }. A Unity game that drains the link but doesn’t handle Play in game should answer with Reason = PatterPlayInGame.Unhandled, so Patterpad isn’t left waiting.

In Unreal, set Link->OnPlay, which fires on the game thread with an FPatterPlayRequest, and answer with Link->ReplyPlay(Request.Id, Result).

In Godot, connect the link’s play_requested(request) signal and answer with link.reply_play(request.id, { "started": true }).

In JavaScript, Unreal, and Godot, a link with no handler answers unhandled itself.

Every engine’s client has the same shape: open it with the build id, tell it when a flow opens, report the position after each advance() / choose(), and tell it when a flow closes. It never throws into your game loop, and if Patterpad isn’t listening every call is a no-op.

JavaScript: @patterkit/play-helpers ships createDebugLink:

import { createDebugLink } from "@patterkit/play-helpers";
const link = createDebugLink({
build: engine.buildId, // the build identity: your compiled bundle's content hash
project: "My Game", // shown in the editor's debug-link tooltip (optional)
// url: "ws://127.0.0.1:4471", // the default; override if you changed the port
});
link.flowOpened("main"); // optional: lists the flow before its first step
// ...in your play loop, after each step:
const step = flow.advance();
link.observe("main", flow.currentScene, step.id ?? null, step.type);
link.flowClosed("main"); // ...and when the flow ends

observe is the only call you must make. It carries the flow’s id, so a flow the link has not seen announces itself on its first step: forgetting flowOpened costs you the flow’s row in the follow list until it moves, not for the whole session. flowOpened is still worth calling for a flow that exists before it says anything, and flowClosed still matters, since nothing else tells the editor a flow has finished.

Unity. new PatterDebugLink(...). Wire it behind #if UNITY_EDITOR || DEVELOPMENT_BUILD so it is stripped from a release player build:

#if UNITY_EDITOR || DEVELOPMENT_BUILD
_link = new PatterDebugLink(engine.BuildId, "My Game");
_link.FlowOpened("main");
// ...after each step:
_link.Observe("main", flow.CurrentScene, step.Id, PatterDebugLink.TypeName(step.Type));
#endif

Unreal. FPatterDebugLink::Create(...). It compiles to no-ops in a Shipping build (the WebSockets dependency is dropped there), so it is safe to leave in:

Link = FPatterDebugLink::Create(Engine->BuildId(), TEXT("My Game"));
Link->FlowOpened(TEXT("main"));
// ...after each step (map EPatterStepType -> "line" / "text" / "gameEvent" / "choice" / "end"):
Link->Observe(TEXT("main"), Flow->CurrentScene(), Step.Id, FPatterDebugLink::TypeName(Step.Type));

Godot. A PatterDebugLink node. It only opens the link in a debug build (OS.is_debug_build()), so it is inert in a release export:

var link := PatterDebugLink.new(engine.build_id(), "My Game")
add_child(link)
link.flow_opened("main")
# ...after each step:
link.observe("main", flow.current_scene(), step.get("id", ""), step["type"])

Each link also says what it is doing, which matters because from inside a running game “the editor isn’t listening” and “I never attached” look the same: its state (connecting, connected, or closed), the build it handshook with, and the address it dials. That’s link.state, link.build, and link.url in JavaScript, State, Build, and Url in Unity, State(), Build(), and Url() in Unreal, and state(), build(), and url() in Godot.

That’s the whole integration on any engine. Leave the client wired behind your engine’s debug flag and it costs nothing in a shipped game.

For native ports or a custom client, the protocol is one small JSON object per message over the WebSocket:

{ "t": "hello", "v": 1, "build": "<bundle hash>", "project": "My Game", "flows": ["main"] }
{ "t": "frame", "flow": "main", "sceneId": "<scene id>", "beatId": "<beat id|null>", "type": "line|text|gameEvent|choice|end" }
{ "t": "frame", "flow": "main", "sceneId": "<scene id>", "beatId": "<choice id>", "type": "choice", "choiceId": "<option id>" }
{ "t": "flowOpen", "flow": "main" }
{ "t": "flowClose", "flow": "main" }

A choice frame sent again with choiceId set to the option taken marks that option in the editor. Every client’s observe takes it as an optional last argument.

Two messages travel the OTHER way, editor to game (live bundle refresh and Play in game, above):

{ "t": "bundle", "v": 1, "build": "<new bundle hash>", "data": "<the full .patterc JSON>" }
{ "t": "play", "v": 1, "id": "<request id>", "sceneId": "<scene id>", "blockId": "<block id>", "takeOver": false, "build": "<bundle hash>" }

The game answers play with:

{ "t": "played", "id": "<request id>", "started": false, "reason": "busy", "detail": "Mara is talking." }

Send hello first; the editor reads the build + flow list from it before honouring any frames. When the socket closes, try again after 1 second, doubling to at most 10 seconds, and back to 1 second once a hello goes; each new socket says hello again, with the open flows. Queue frames only while the first socket connects. The one close not to retry is code 4000 ("replaced"): the editor sends it when another game connects. The ids are the bundle’s opaque model ids: the same ones every runtime already exposes on its step result and currentScene. The server binds to 127.0.0.1 only, so no pairing token is needed. It refuses a connection whose Origin header names any host but localhost, 127.0.0.1 or ::1; a native client sends no Origin, or (Unreal) the loopback address, and both are let in.

Open source under the MIT licence, made by .