# Language syntax

The Dialogue language lets you write branching conversations as text inside **[Scripts](scripts.md)**. Start with lines of speech, then add choices, Conditions, Variables, and Commands as your story needs them.

!!! tip "Use the cheatsheet for lookup"
    If you already understand the language, download the **[quick lookup cheatsheet](assets/cheatsheet.pdf)** for a compact syntax reference.

!!! warning "Indentation is structure, not decoration"
    Two spaces of indentation put a line inside the line above it. Dialogue uses that structure to group lines under choices and conditions.

    ```dialogue
    @if has-money
      Player: I have some money.
    @else
      Player: I have no money.
    ```

    The second line is indented, so it belongs to `@if has-money`. The last line belongs to `@else`. Exactly one of them plays.

    Incorrect indentation can run a valid line under the wrong branch. Check its spaces first when a branch runs unexpected content.

## Text lines

A Script is plain text, and each non-empty line defines content. To write a conversation, put each line of speech on its own line:

```dialogue
This is the first line.
This is the second line.
This is the third line.
```

Dialogue displays these lines from top to bottom.

![Three Script lines displayed in the game UI](assets/syntax-ui.jpg)

!!! tip "Blank lines are free"
    Dialogue skips empty lines. Use them to separate paragraphs and make long Scripts easier to scan.

### Actors

Write an Actor ID, a colon, and the text to assign a line to that character:

```dialogue
Jessie: Prepare for trouble!
James: And make it double!

Jessie: To protect the world from devastation!
James: To unite all peoples within our nation!

Jessie: To denounce the evils of truth and love!
James: To extend our reach to the stars above!

Jessie: Team Rocket, blast off at the speed of light!
James: Surrender now, or prepare to fight!

Meowth: Meowth, that's right!
```

A **[Compendium](compendiums.md)** maps each Actor ID to its display name, description, color, portrait, and scene game object. The Script stores only the ID.

### Expressions

An Actor can use an **Expression** to change its portrait and other mood-specific UI. Add a period and the Expression ID after the Actor ID:

```dialogue
King.Concerned: Sentry! For what reason art thou derelict in thy duty?
Guard.Surprised: Zzz... Hngh...! My liege! I meant no neglect!
```

!!! tip "An Expression covers only the line that names it"
    Each line shows the Expression it names, or the Actor's plain look when it names none. To hold a mood, repeat the Expression on every line. Consecutive lines from that Actor with the same Expression keep it active, so its **On Enter** and **On Exit** Instructions run once for the whole stretch.

    Here **Angry** covers the first four lines and ends on the last, which names no Expression:

    ```dialogue
    Walter.Angry: I am not in danger, Skyler!
    Walter.Angry: I AM THE DANGER.
    Walter.Angry: A guy opens his door and gets shot.
    Walter.Angry: And you think that of me?
    Walter: No, Skyler... I am the one who knocks!
    ```

## Comments

A line that starts with `//` is a comment. Dialogue ignores it at runtime, so you can leave notes for yourself or another writer without showing them to the player:

```dialogue
// This is a comment. It won't appear in the game.
Player: Elementary, my dear Watson.
```

## Choices

A line that starts with an asterisk (`*`) defines a choice the player can select:

```dialogue
Player: Hmm... What should I do next?

* I have a few more questions.
* I want to explore the town.
* I want to interrogate the suspect.
```

!!! tip "Choices take Actors and Expressions too"
    A choice accepts the same Actor and Expression syntax as a text line. Use it when the UI should show how the player character will deliver the choice:

    ```dialogue
    Bouncer: You're not on the list. Get lost.

    * Player.Menacing: You better let me in, or... <cracks knuckles>
    * Player.Persuasive: Maybe this will change your mind? <picks a credit pack>
    ```

    A choice's Expression only changes how that choice appears. It doesn't run the Expression's **On Enter** or **On Exit** Instructions, and it doesn't end an Expression the Actor's text lines left active.

Lines indented two spaces under a choice run only when the player selects that choice:

```dialogue
Player: Hmm... What should I do next?

* I have a few more questions.
  Witness: Of course! You may ask anything!
  ...

* I want to explore the town.
  Witness: Please, allow me to show you out.

* I want to interrogate the suspect.
  Witness: You will find him tied in the other room.
```

When a choice branch finishes, the Script continues after the complete choice group. Unindented lines below the group therefore run after every branch:

```dialogue
Morpheus: Will you take the red pill or the blue pill?

* The red pill
  Morpheus: If you take it you will see the truth about the Matrix.

* The blue pill
  Morpheus: If you take it you will return to your life.

// These lines run after either choice.
Morpheus: Neo, you are the Chosen One.
Morpheus: Fate already flipped that coin for you.
Morpheus: Get ready to be unplugged.
```

### Conditions on a choice

Add `@if` and a Condition ID to a choice to control whether the player can select it:

!!! warning "One Condition per choice"
    A choice takes a single Condition identifier. Combine multiple checks inside that Condition.

```dialogue
* @if has-gold-200 I'd like to buy that.
  Merchant: Of course! A pleasure doing business with you.

* I can't afford that right now.
  Merchant: Come back when you can afford it then.
```

A choice whose Condition fails can't be selected. In the **[Script](scripts.md)** and **Dialogue Settings**, you choose whether the player sees it disabled or doesn't see it at all.

## Labels

A line that starts with a hash (`#`) defines a **Label**: a named point in the Script that you can jump to with a Command.

!!! info "Labels use restricted identifiers"
    Label IDs accept letters, digits, hyphens (`-`), and underscores (`_`). They cannot contain spaces.

```dialogue
# start
Narrator: The sky above the port was the color of TV.
Narrator: Tuned to a dead channel.
```

The `@go` command jumps to a Label:

```dialogue
# start

DrStrange: Dormammu!
DrStrange: I've come to bargain!

@go start
```

!!! tip "A question loop is one label and one @go"
    Letting the player ask a character anything, in any order, as many times as they like, is the shape most investigation scenes take. Put a label above the choices and send each branch back to it — with one choice reserved for leaving.

    ```dialogue
    # questions
    Witness: Do you want to ask me anything else?

    * Where were you at midnight?
      Witness: I was doing the dishes, as Lord Byron ordered.
      @go questions

    * What are your duties here?
      Witness: I am Lord Byron's servant, sir.
      @go questions

    * Do you hold any grudges against Lord Byron?
      Witness: No sir! He has always been kind to me.
      @go questions

    * No more questions.
      Witness: Let me know if you need anything else.
    ```

## Commands

Anything starting with `@` is a **Command**.

### `@run`

`@run` executes a Command defined in a Compendium, such as giving the player an item, starting a quest, or playing an effect:

```dialogue
Townfolk: Thank you for taking care of that Banshee!
Townfolk: Please, take this as a reward for your efforts.

@run give-player-sword
```

The Script starts the Command and continues to the next line without waiting. Add `await` when the conversation must wait for completion:

```dialogue
Player: We won't get far if we attack during daylight.
Player: We'll wait here till the sun sets.

@run await change-night-time

Player: It's getting dark. We should get moving.
```

Run a sound effect without `await` when it should overlap the next line. Add `await` to a day-to-night transition when the next line depends on its completion.

### `@wait`

`@wait` pauses the conversation for a number of seconds:

```dialogue
Kasparov: Queen to D5. Check.
Player: (Urgh! It's like he can read my mind and plan 5 steps ahead!)
Player: (What can I do? Think..! Think...!)
Player: (Wait a second...)

@wait 3.5

Player: Not so fast! Knight to A7! Checkmate!
Kasparov: N-NANI...?!
```

Add `unscaled` between the Command and the duration when the wait should continue even if you pause the game's time scale:

```dialogue
@wait unscaled 2.5
```

### `@exit`

`@exit` stops the Script immediately and skips every remaining line:

```dialogue
Bouncer: You're not on the list. Get lost.

* [Threaten] You'd better let me in.
  Bouncer: That's it. We're done talking.
  @exit

* [Bribe] Maybe this will change your mind?
  Bouncer: I'm listening...
```

### `@script`

`@script` runs another Script as though its lines appeared at that point. Use it to split a long story into navigable files or reuse one sequence from several Scripts:

```dialogue
Player: If you don't mind, I have some questions I would like to ask.

@script ask-questions

Player: Thank you for your time. That was very helpful.
```

By default, the referenced Script starts at the top. Add a Label ID after the Script ID to start from that Label:

```dialogue
@script ask-questions personal-questions
```

## Conditions

`@if` runs its indented block when all named Conditions are true:

```dialogue
@if has-met-npc
  Player: It's good to see you again!
```

!!! tip "Several conditions on one line means AND"
    `@if` and `@or` accept multiple Condition IDs separated by spaces. Every Condition on that line must be true.

    ```dialogue
    @if has-sword has-shield
      Player: I swear I'll destroy you with my shield and sword!
    ```

When an `@if` branch fails, the Script skips its block. `@else` provides a fallback branch:

```dialogue
@if has-met-npc
  Player: It's good to see you again!

@else
  Player: Hello! Pleased to make your acquaintance.
```

`@or` adds another branch. It runs when every preceding `@if` or `@or` branch fails and its own Conditions are true.

!!! tip "@or behaves like else if"
    Use `@or` for the same branch structure that C# expresses with `else if`:

```dialogue
Merchant: This sword? Sorry but I can't sell it to you.
Player: Look, we both know everything has a price.

@if has-gold-200
  Player: So what do you think, huh? 200 gold pieces?

@or has-diamond
  Player: So, is this diamond shiny enough to persuade you?

@else
  Player: I... Huh... I promise I'll pay you one day?
```

## Selections

`@select` runs one of the lines indented under it, then continues after the whole block. Use it for lines that should change between visits, such as a shopkeeper's greeting or a guard's idle remark:

```dialogue
@select random
  Shopkeeper: Welcome, traveler!
  Shopkeeper: Back again? Take a look around.
  Shopkeeper: Mind the merchandise.

Shopkeeper: What can I get you?
```

Each indented line is an **option**. When the Script reaches `@select`, it picks one greeting, skips the other two, and continues with *What can I get you?*

An option can have its own indented lines. They play only when that option is picked:

```dialogue
@select random
  Bard: Have you heard the tale of the dragon slayer?
    Bard: No? Then sit down, it's a long one.
  Bard: A coin for a song, friend?
```

Any line can be an option, including Commands. An option with `@go`, for example, jumps to a Label, which turns `@select` into a random branch. A choice (`*`) can't be an option, and the Script shows an error if you try. To offer choices in one option, indent them under a text line.

### Modes

The word after `@select` is its **mode**, which decides how the option is picked. Without a mode, `@select` uses `random`.

| Mode               | Picks                                                                     | Once every option has played                         |
| :--- | :--- | :--- |
| `random`           | Any option. The same one can come up again.                               | Never runs out.                                      |
| `random-no-repeat` | Any option except the last one picked, unless it's the only one available. | Never runs out.                                      |
| `shuffle`          | Each option once, in random order.                                        | Picks nothing.                                       |
| `shuffle-loop`     | Each option once, in random order.                                        | Starts a new round that doesn't begin with the option that just played. |
| `sequence`         | Each option once, from top to bottom.                                     | Picks nothing.                                       |
| `sequence-loop`    | Each option once, from top to bottom.                                     | Starts again from the top.                           |
| `sequence-hold`    | Each option once, from top to bottom.                                     | Keeps picking the last one.                          |

`sequence-hold` suits a character whose greeting changes as the player gets to know them:

```dialogue
@select sequence-hold [id: blacksmith-greeting]
  Blacksmith: A new face! Welcome to my forge.
  Blacksmith: You again? Need something sharpened?
  Blacksmith: The usual?
```

The first visit plays the welcome and the second plays the follow-up. Every visit after that plays *The usual?*

### Saving progress

Every mode except `random` remembers what it has picked. That progress belongs to the **[Dialogue](dialogue.md)** component that plays the Script, so two characters that share one Script keep track separately.

Give the `@select` an `id` in its [metadata](#metadata) to save that progress with the Dialogue's **[Remember](dialogue.md#saving-and-loading)** component. The `id` must be unique within the Script, like a line's. Without one, the progress lasts only as long as the Dialogue component and is never saved.

!!! tip "Generate IDs covers selections too"
    **Generate IDs**, in the Script's **[Settings](scripts.md#settings)** tab, adds an `id` to every `@select` that doesn't have one, along with your text lines and choices.

### Conditional options

`@if`, `@or`, and `@else` lines placed directly under a `@select` don't run as branches. Instead, the lines indented under them become options only while their Conditions are true:

```dialogue
@select random
  @if is-night
    Guard: Quiet. People are sleeping.
    Guard: Keep your torch low.
  Guard: Move along.
```

At night, the guard picks one of the three lines. During the day, only *Move along.* can come up.

!!! warning "Under a @select, an @if holds alternatives"
    Each line under the `@if` is its own option, so only one of them plays. To play several lines together, indent them under one option:

    ```dialogue
    @select random
      @if is-night
        Guard: Quiet. People are sleeping.
          Guard: Keep your torch low.
      Guard: Move along.
    ```

    *Keep your torch low.* now always follows *Quiet. People are sleeping.*

An option whose Condition is false is skipped for now, not forgotten. Modes that play each option once still play it on a later visit, once its Condition is true.

### When nothing can be picked

When no option can be picked, the Script skips the whole block. That happens when every option's Condition is false, or when a `shuffle` or `sequence` has already played all of them. Put an `@else` right after the block to run something in that case:

```dialogue
@select shuffle [id: tavern-rumors]
  Innkeeper: They say the old mill is haunted.
  Innkeeper: A dragon was seen over the northern hills.
@else
  Innkeeper: I'm all out of rumors, I'm afraid.
```

The innkeeper shares each rumor once, in random order. Once both have played, the `@else` branch runs instead. An `@or` after the block works the same way.

### Weights

Add a `weight` to an option's metadata to change how often the modes that pick at random choose it. An option without one weighs `1`:

```dialogue
@select random
  Parrot: Squawk!
  Parrot: Polly wants a cracker!
  Parrot: Pieces of eight! [weight: 0.2]
```

*Pieces of eight!* comes up five times less often than each of the other two lines. In `shuffle` modes, every option still plays once per round, and a heavier option tends to come earlier.

## Variables

{% raw %}
A **Variable** wrapped in double curly braces `{{ }}` is replaced by its value when the line plays:
{% endraw %}

{% raw %}

```dialogue
Merchant: Welcome, {{player-name}}!
Merchant: Oh! I see you have {{gold-amount}} pieces of gold...
```

{% endraw %}

!!! tip "Spaces inside the braces make no difference"
    Both forms work. The convention is to leave them out — it reads tighter, and it gives a translator one less thing to get wrong.

## Metadata

**Metadata** is a set of key/value pairs written at the end of any line, enclosed in square brackets `[ ]`. It overrides how that line behaves, and it passes your own values into game logic.

Entries are separated by commas, and each entry is a `key:value` pair. A space after the colon is optional:

```dialogue
Player: This line has some metadata. [id:17d6s9, mood:happy]
```

Read these values with the **Active Line Metadata** [Visual Script](https://visualscript.dev) Signals. This lets you pass different parameters to the same Command or Condition from different lines.

!!! example "One condition instead of dozens"
    A dedicated Condition can check for `200` gold:

    ```dialogue
    @if has-gold-200
      Player: I have at least 200 pieces of gold.
    ```

    Different thresholds would require near-identical Conditions. Metadata instead passes the threshold as a parameter, so one generic `has-gold` Condition covers every value:

    ```dialogue
    @if has-gold [minimum: 200]
      Player: I have at least 200 pieces of gold.
    ```

    Each new check changes a number in the Script instead of adding another Condition asset.

### Reserved keywords

`id` is reserved for identifying a line, and a handful of other keys change how particular kinds of line behave.

For texts:

| Keyword    | Changes                                             | Values                                           |
| :--- | :--- | :--- |
| `id`       | The line's unique identifier.                       | Any unique string.                               |
| `continue` | What advances to the next line.                     | `0`: Interact<br>`1`: Timeout<br>`2`: Wait Audio |
| `timeout`  | How long to wait before advancing automatically.    | Any positive number, such as `3.5`.              |
| `unscaled` | Whether that timeout ignores the game's time scale. | `1` or `0`.                                      |

For choices:

| Keyword           | Changes                                                    | Values                                                                   |
| :--- | :--- | :--- |
| `id`              | The choice's unique identifier.                            | Any unique string.                                                       |
| `shuffle`         | Whether the order is randomized.                           | `1`: Shuffle<br>`0`: Do not shuffle                                      |
| `timed`           | Whether the group is on a countdown.                       | `1`: Timed<br>`0`: Not timed                                             |
| `duration`        | How long the player has to choose.                         | Any positive number.                                                     |
| `unscaled`        | Whether that countdown ignores the game's time scale.      | `1` or `0`                                                               |
| `timeout`         | What happens when it runs out.                             | `0`: Random choice<br>`1`: First choice<br>`2`: Last choice<br>`3`: Skip |
| `hideUnavailable` | Whether an unavailable choice is hidden or shown disabled. | `1`: Hide<br>`0`: Show                                                   |
| `autoPickSingle`  | Whether a lone available choice is taken automatically.    | `1`: Enable<br>`0`: Disable                                              |

For `@select`:

| Keyword  | Changes                                                                          | Values                                               |
| :--- | :--- | :--- |
| `id`     | The identifier its progress is saved under. Goes on the `@select` line.          | Any unique string.                                   |
| `weight` | How often the modes that pick at random choose an option. Goes on the option.    | Any positive number, such as `2.5`. Defaults to `1`. |

## Where to go next

- **[Scripts](scripts.md)** — create the asset that stores dialogue text and its runtime settings.
- **[Compendiums](compendiums.md)** — define the Actors, Variables, Commands, Conditions, and Scripts used by the language.
- **[Localization](localization.md)** — translate text lines by their metadata IDs.
