# misc/dialogue (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/dialogue/

Dialogue runner.

## DialogueRunner extends Object

Dialogue runner.
- Interprets in order a script made of {speaker, text} lines and choices / labels / jumps.
  Standardizes the state machine that visual novels / RPG dialogues / tutorials each built.
- Script entry format:
    { speaker: "name", text: "line" }
    { label: "tag" }
    { jump: "tag" }
    { choice: [{ text: "option", jump: "tag" }, ...] }
    { end: true }
- Usage:
    runner.setScript(entries); runner.start();
    runner.getCurrent()   → { kind: "line" | "choice" | "finished", ... }
    runner.advance() on a line, runner.choose(index) on a choice.

Example: Advance lines and branch on a choice

```js
import { DialogueRunner } from "./build/vanilla.js";

const dialogueRunner = new DialogueRunner();
dialogueRunner.setScript([
	{ speaker: "Guide", text: "Welcome, traveler." },
	{ choice: [{ text: "Tell me more", jump: "more" }, { text: "Bye", jump: "bye" }] },
	{ label: "more" },
	{ speaker: "Guide", text: "This village is old." },
	{ end: true },
	{ label: "bye" },
	{ speaker: "Guide", text: "Safe travels." },
]);
dialogueRunner.start();
let current = dialogueRunner.getCurrent();
console.log(current.kind, current.speaker, current.text);

dialogueRunner.advance();
current = dialogueRunner.getCurrent();
console.log(current.kind, current.options.length);

dialogueRunner.choose(1);
current = dialogueRunner.getCurrent();
console.log(current.kind, current.text);

dialogueRunner.advance();
console.log(dialogueRunner.isFinished());
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `setScript(entryList)` (method, since 0.3.0): Sets the script.
  - `entryList` object[]: List of script entries. (an empty script if null)
- `start(labelName = null)` (method, since 0.3.0): Start. (from that point if a label is passed)
  - `labelName` string | null: Label to start from. (from the beginning if null or an unknown label)
- `getCurrent()` (method, since 0.3.0): Returns the current entry.
  - { kind: "line", speaker, text } | { kind: "choice", options: [{text}] } | { kind: "finished" }
  - returns object: Entry to show now.
- `advance()` (method, since 0.3.0): Advances. (called on a line entry, ignored on a choice)
- `choose(optionIndex)` (method, since 0.3.0): Chooses. (called on a choice entry)
  - `optionIndex` number: Index of the option to choose. (from 0, ignored if out of range)
- `isFinished()` (method, since 0.3.0): Whether ended.
  - returns boolean: True when the script has ended. (also true before start)

## DialogueScriptParser extends Object

Script parser.
- Converts one-line command text into a DialogueRunner script. Format:
    /label tag
    /jump tag
    /speaker speakerName            (default speaker for subsequent /say)
    /say line text
    /option option text -> tag   (consecutive /option lines are grouped into one choice)
    /end
  Lines not starting with "/" are treated as /say by the previous speaker. "//" is a comment.

Example: Parse a command script and feed it to the runner

```js
import { DialogueScriptParser, DialogueRunner } from "./build/vanilla.js";

const scriptText = [
	"/speaker Guide",
	"/say Welcome, traveler.",
	"Need a hand?",
	"/option Yes -> help",
	"/option No -> bye",
	"/label help",
	"/say Follow the river.",
	"/end",
	"/label bye",
	"/say Safe travels.",
].join("\n");
const entryList = DialogueScriptParser.parse(scriptText);
console.log(entryList.length);

const dialogueRunner = new DialogueRunner();
dialogueRunner.setScript(entryList);
dialogueRunner.start("help");
const current = dialogueRunner.getCurrent();
console.log(current.speaker, current.text);
```

- `static parse(scriptText)` (static, since 0.3.0): Parse. (static)
  - `scriptText` string: Command script text. (one command per line)
  - returns object[]: DialogueRunner script.
