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.

The Dialogue Runner doesn’t show anything on screen. The Presenters do that.

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.
PLACEHOLDER — Screenshot needed
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.

Most games need one Dialogue Runner. It runs one conversation at a time, so if you need 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. It doesn’t look through its children for them, so a Presenter that isn’t in the list is never used. In the Dialogue System prefab, the list holds the Line Presenter, the Options Presenter and the Line Advancer.

PLACEHOLDER — Screenshot needed
The Dialogue Runner’s Dialogue Presenters list, with the Line Presenter, Options Presenter and Line Advancer assigned.

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 as soon as the scene starts. 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, and the Events section, 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 void TalkToRosa()
    {
        dialogueRunner.StartDialogue("Lighthouse");
    }
}
Line 6 The Dialogue Runner. Drag it into this field in the Inspector.
Lines 8–11 Call StartDialogue() with the name of the node to start from, for example when the player walks up to Rosa and presses a button.
Starting dialogue from code.
  • StartDialogue(nodeName) starts from the node you name. You always have to name a node; Start Node is only used by Start Automatically.
  • Stop() ends the dialogue. The Presenters finish up, and On Dialogue Complete is invoked.
  • IsDialogueRunning is true while dialogue is running. Use it to stop the player walking off mid-conversation.

StartDialogue() and Stop() are async, so you can await them. StartDialogue() finishes once the dialogue has started, and Stop() once the Presenters have finished with it. To wait until a whole conversation has ended, await the Dialogue Runner’s DialogueTask.

If you call StartDialogue() while dialogue is already running, the Dialogue Runner jumps straight to the new node. It doesn’t end the first conversation, so On Dialogue Complete isn’t invoked for it. To end it first, await dialogueRunner.Stop() before starting the new node.

Events

The Dialogue Runner invokes these events as the dialogue runs. You’ll find them in the Events section at the bottom of the Inspector, which starts folded up. Connect them to your own methods there, or from code.

EventWhen it’s invoked
On Dialogue StartDialogue has started.
On Dialogue CompleteDialogue has ended, either at the end of the dialogue or because Stop() was called.
On Node StartThe Dialogue Runner has entered a node. Passes the node’s name.
On Node CompleteThe Dialogue Runner has left a node. Passes the node’s name.
On Unhandled CommandA Command didn’t match anything you’ve registered. Passes the command’s text. See Commands.
The Dialogue Runner’s events.
PLACEHOLDER — Screenshot needed
The Events section of a Dialogue Runner’s Inspector, unfolded, showing On Node Start, On Node Complete, On Dialogue Start, On Dialogue Complete and On Unhandled Command.

For example, to stop the player moving while they’re in a conversation:

void Start()
{
    dialogueRunner.onDialogueStart.AddListener(() => player.canMove = false);
    dialogueRunner.onDialogueComplete.AddListener(() => player.canMove = true);
}
Stopping the player moving while dialogue runs.

When a Command doesn’t match anything you’ve registered, the dialogue stops there and doesn’t move on. This happens whether the Dialogue Runner logs an error or invokes On Unhandled Command. A misspelt Command name, or a Command you only handle through On Unhandled Command, leaves the conversation stuck. Register every Command your scripts use with [YarnCommand] or AddCommandHandler().

On Unhandled Command only takes over from the Dialogue Runner when something is connected to it in the Inspector. Listeners you add from code with AddListener() aren’t counted, and the Dialogue Runner logs an error for the unknown Command instead.

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:

  • RequestHurryUpLine() asks the current line to finish appearing at once, such as by skipping the typing effect. The line stays on screen.
  • RequestNextLine() asks the Presenters to finish with the current line and move on.
  • RequestHurryUpOption() asks the options to finish appearing at once.

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.