Dialogue Runner

The Dialogue Runner is the component that runs your dialogue. You give it a Yarn Project, and it steps through your Yarn Scripts one line at a time. It hands each line and set of options to your Dialogue Presenters, calls your Commands, and keeps track of your variables.

Everything the player sees on screen comes from the Presenters.

Adding a Dialogue Runner

The quickest way to get a Dialogue Runner is the Dialogue System prefab. Open the GameObject menu and choose Yarn SpinnerDialogue System. It comes with a Dialogue Runner, Presenters and a Line Advancer, already connected. See Showing Dialogue.

To add a Dialogue Runner to a GameObject of your own instead, click Add Component in the Inspector and search for Dialogue Runner.

  1. Select the Dialogue Runner, and set Yarn Project to your .yarnproject, by dragging it in from the Project window.
  2. If you want the dialogue to start on its own, turn on Start Automatically, and choose which node to start from in Start Node. It’s a dropdown of the nodes in your Yarn Project, and defaults to Start.
The Inspector for a Dialogue Runner, with Yarn Project set to a .yarnproject, Start Automatically turned on, and the Start Node dropdown set to Start.
A Dialogue Runner in the Inspector.

Most games need one Dialogue Runner. Each one runs one conversation at a time. For two conversations running at once, such as background chatter while the player talks to someone, use a second Dialogue Runner. The Background Chatter sample does this.

Adding Presenters

The Dialogue Runner can’t show anything until it has Presenters. Most games start with two: a Line Presenter for lines, and an Options Presenter for options. The Dialogue System prefab has both, along with a Line Advancer:

Dialogue System Dialogue RunnerIn Memory Variable Storage
Canvas CanvasCanvas ScalerGraphic Raycaster
Line Presenter Line PresenterCanvas Group
Options Presenter Options PresenterCanvas Group
Line Advancer Line Advancer
The Presenters in the Dialogue System prefab, under its Canvas.

The Dialogue Runner only talks to Presenters in its Dialogue Presenters list. A Presenter that isn’t in the list is never used, even if it’s a child of the Dialogue Runner. The one exception is the Line Advancer, which adds itself to the list when the scene starts. In the Dialogue System prefab, the list holds the Line Presenter, the Options Presenter and the Line Advancer.

The Dialogue Runner's Dialogue Presenters list, with the Line Presenter, Options Presenter and Line Advancer assigned.
The Dialogue Presenters list.

To add a Presenter of your own, click + under the list, and drag the GameObject with the Presenter on it into the new row. From code, set the Dialogue Runner’s DialoguePresenters property to a new list.

Line Presenter and Options Presenter explain how to set up each Presenter, change how text appears, and style the option buttons.

The Dialogue Runner’s settings

SettingWhat it does
Yarn ProjectThe compiled Yarn Project to run.
Variable StorageWhere your Yarn variables are kept. Leave it empty and the Dialogue Runner adds an In Memory Variable Storage when the game runs. See Variables and Storage.
Line ProviderLooks up the text for each line, including translations. Leave it empty and the Dialogue Runner adds a Built-in Localised Line Provider when the game runs. See Line and Asset Providers.
Saliency StrategyHow the Dialogue Runner chooses between the nodes in a node group, and the lines in a line group. See Saliency and Storylets.
Dialogue PresentersThe Dialogue Presenters that show your dialogue.
Verbose LoggingPrints details of what the Dialogue Runner is doing to the Console, every time it enters a node and more, for tracking down problems. Off by default.
Start AutomaticallyStarts dialogue from Start Node when the scene starts, after waiting one frame. Off by default.
Start NodeThe node to start from when Start Automatically is on. Only shown when it’s on. Defaults to Start.
The Dialogue Runner’s main settings.

The settings for options are described below.

Starting and stopping dialogue

Turn on Start Automatically and the dialogue starts on its own when the scene starts, from the node set in Start Node.

Otherwise, start it from your own code, for example when the player talks to a character:

using UnityEngine;
using Yarn.Unity;

public class Rosa : MonoBehaviour
{
    [SerializeField] DialogueRunner dialogueRunner;

    public async YarnTask TalkToRosa()
    {
        await dialogueRunner.RunDialogue("Lighthouse");
    }
}
Line 6 The Dialogue Runner. Drag it into this field in the Inspector.
Lines 8–11 await RunDialogue() with the name of the node to start from, for example when the player walks up to Rosa and presses a button. Code after the await runs once the whole conversation has ended.
Starting dialogue from code.
  • RunDialogue(nodeName) starts from the node you name, and finishes once the whole conversation has ended. You always have to name a node, because Start Node is only used by Start Automatically.
  • Stop() ends the dialogue. It’s async too, and finishes once the Presenters have finished with the dialogue.
  • IsDialogueRunning is true while dialogue is running. Use it to stop the player walking off mid-conversation.

To do something when a conversation ends, put it after the await:

public async YarnTask TalkToRosa()
{
    player.canMove = false;
    await dialogueRunner.RunDialogue("Lighthouse");
    player.canMove = true;
}
Stopping the player moving while dialogue runs.
If you call RunDialogue() while dialogue is already running, the Dialogue Runner logs an error and doesn’t start the new node. To end the first conversation, await dialogueRunner.Stop() before starting the new one.

Starting dialogue from a UnityEvent

A UnityEvent, like a button’s On Click, can’t call a method that returns a task, which rules out RunDialogue(). Use StartDialogue(nodeName) there instead. It starts the dialogue and returns straight away, without waiting for the conversation to end. StartDialogue() is marked obsolete, and Unity shows a warning if you call it from code, because code should use RunDialogue(). It’s still there for UnityEvents.

Choosing a node from your own scripts

To get the same node dropdown as Start Node on your own scripts, add the [YarnNode] attribute to a string field, and name the field that holds the Yarn Project:

using Yarn.Unity;
using Yarn.Unity.Attributes;

public YarnProject project;

[YarnNode(nameof(project))]
public string startAt;
A node dropdown on your own script.

The dropdown lists the project’s nodes in alphabetical order. To list only some of them, add a filter:

[YarnNode(nameof(project), filter: "Shop")]
public string shopNode;

[YarnNode(nameof(project), filterType: YarnNodeFilter.StartsWith, filter: "Intro_")]
public string introNode;

[YarnNode(nameof(project), filterHeader: "tags", filter: "chapter2")]
public string chapterNode;
Node dropdowns that only list some of the nodes.
SettingWhat it does
filterThe text to look for.
filterTypeYarnNodeFilter.Contains (the default), StartsWith, EndsWith, or MatchesRegex for a regular expression. The first three ignore upper and lower case. MatchesRegex doesn’t.
filterHeaderWhich header to look in. The default is title, the node’s name. Use tags, color, or any header your nodes have.
The settings for filtering a node dropdown.

Reacting to dialogue

The Dialogue Runner doesn’t have events to connect in the Inspector. Your code finds out what’s happening in two ways:

  • To know when a conversation has ended, await RunDialogue(), as shown above.
  • To know about everything else, write a small Presenter and add it to the Dialogue Runner’s Dialogue Presenters. The Dialogue Runner calls its methods as the dialogue runs, and you only write the ones you need.
Presenter methodWhen it’s called
OnDialogueStartedAsync(token)Dialogue has started.
OnDialogueCompleteAsync(token)Dialogue has ended, either at the end of the dialogue or because Stop() was called.
OnNodeEnter(nodeName, token)The Dialogue Runner has entered a node.
OnNodeExit(nodeName, token)The Dialogue Runner has left a node.
RunCommand(command, token)The dialogue has reached a Command, whether or not anything handles it.
The Presenter methods the Dialogue Runner calls as dialogue runs.

For example, this Presenter stops the player moving while they’re in any conversation, however it was started:

using System.Threading;
using UnityEngine;
using Yarn.Unity;

public class PlayerLock : DialoguePresenterBase
{
    [SerializeField] Player player;

    public override YarnTask OnDialogueStartedAsync(CancellationToken token)
    {
        player.canMove = false;
        return YarnTask.CompletedTask;
    }

    public override YarnTask OnDialogueCompleteAsync(CancellationToken token)
    {
        player.canMove = true;
        return YarnTask.CompletedTask;
    }
}
A Presenter that stops the player moving while dialogue runs.

Add the component to a GameObject, and drag it into the Dialogue Runner’s Dialogue Presenters list. You can also add it from code with dialogueRunner.AddPresenter().

When a Command doesn’t match anything you’ve registered, the Dialogue Runner logs a message, and the dialogue carries on with the next line. If your game has added any Commands from code, the message is Encountered undispatched command. If it hasn’t, the message is the error The command dispatcher has not been configured to handle the Command. A misspelt Command name is skipped in the same way. Check the Console if a Command doesn’t seem to do anything. To handle Commands yourself, write a Presenter with RunCommand().

How it works with Presenters

When the Dialogue Runner reaches a line, it gives the line to every Presenter in its list at the same time. It waits until all of them have finished with it, then moves on. A Line Presenter and a Voice Over Presenter can work on the same line, and the Dialogue Runner continues only once the text has been dismissed and the clip has finished.

When it reaches a set of options, it gives them to every Presenter in the same way, and uses the first option any of them picks.

The Line Advancer tells the Dialogue Runner to move on when the player presses a button. If you’re building your own controls, call these yourself:

  • RequestHurryUpContent() asks the current content to finish appearing at once, such as by skipping a line’s typing effect or making options appear without fading in. A line stays on screen.
  • RequestNextContent() asks the Presenters to finish with the current line and move on.

Both also reach a Command that’s running, if it takes a cancellation token. The older RequestHurryUpLine(), RequestHurryUpOption() and RequestNextLine() still work, but they’re obsolete.

Options

These settings change how options work.

SettingDefaultWhat it does
Run Selected Option As LineOffAfter the player picks an option, show its text as a line of dialogue before continuing. In the Behaviour section, under Start Automatically.
Allow Option FallthroughOnIf none of your Presenters picks an option, carry on with the dialogue after the options. Below the Behaviour section.
The settings for options.
If Allow Option Fallthrough is off and none of your Presenters picks an option, the Dialogue Runner logs an error and the dialogue stops moving. The usual cause is an Options Presenter missing from the Dialogue Presenters list.

Saving and loading

The Dialogue Runner can save your Yarn variables to a file in Unity’s persistent data path, and load them back:

dialogueRunner.SaveStateToPersistentStorage("dialogue_save.json");
dialogueRunner.LoadStateFromPersistentStorage("dialogue_save.json");
Saving the dialogue’s state, and loading it again.

Both return true if they worked. They save and load the variables in the Dialogue Runner’s Variable Storage. See Variables and Storage for other ways to save your variables along with the rest of your game.

Next step Line Presenter Showing each line on screen, with the speaker's name, and continuing to the next one.