Putting It Together

Commands let your Yarn Scripts tell your game to do something. Functions let them ask your game for a value. The scene below uses both.

In the scene, Rosa hears the door, walks over to the lamp and lights it, then gives the player a key if they don’t already have one. The inventory in the top right shows the key once she’s handed it over.

The demo scene running.

The scene

Main Camera CameraAudio Listener
Directional Light Light
Game GameAudio Source
Rosa Mesh FilterMesh RendererCharacter
Lamp Light
Inventory Canvas CanvasCanvas ScalerGraphic Raycaster
Inventory TextMeshPro - Text (UI)
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 scene. Game has the script that adds the Commands and Functions, and Rosa has the script with the move Command.

The Dialogue System comes from GameObjectYarn SpinnerDialogue System. Its Dialogue Runner has Start Automatically turned on, with Start Node set to CommandsFunctionsDemo. The Lamp’s Light starts with its Intensity at 0.

The Yarn Script

title: CommandsFunctionsDemo
---
<<play_sound door_creak>>
Rosa: Oh! You gave me a fright.
Rosa: The lamp's been out for {days_since_lamp_lit()} days.
<<move Rosa 3 0>>
Rosa: Let me get it going again.
<<light_lamp>>
Rosa: There. Much better.
<<if not has_item("brass key")>>
    Rosa: You'll need this to get up the stairs.
    <<give_item "brass key">>
<<endif>>
<<if has_item("brass key")>>
    Rosa: Now you've got the key. Up you go!
<<endif>>
===
Line 3 A Command. The game plays a creaking sound. The dialogue doesn’t wait for it, and Rosa’s first line appears while the sound is still playing.
Line 5 A Function inside a line. days_since_lamp_lit() returns 12, which is shown in the line.
Line 6 A Command on the GameObject called Rosa. The dialogue waits until she has finished walking.
Line 8 A Command that also waits. The lamp fades on before Rosa’s next line.
Line 10 A Function in a condition. has_item("brass key") returns false, so Rosa hands over the key.
Line 12 A Command with an argument in quotes, because it has a space in it. The key appears in the inventory.
Lines 14–16 The same Function again. This time it returns true, because the player now has the key.
The node that runs in the scene.

The game code

The script on the Game GameObject adds the Commands and Functions when the scene starts:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Yarn.Unity;

public class Game : MonoBehaviour
{
    [SerializeField] DialogueRunner dialogueRunner;
    [SerializeField] AudioSource audioSource;
    [SerializeField] AudioClip doorCreak;
    [SerializeField] Light lampLight;
    [SerializeField] TextMeshProUGUI inventoryLabel;

    readonly List<string> inventory = new();

    static private Game Instance => FindAnyObjectByType<Game>();

    void Awake()
    {
        dialogueRunner.AddCommandHandler<string>("play_sound", PlaySound);
        dialogueRunner.AddCommandHandler("light_lamp", LightLamp);
        dialogueRunner.AddCommandHandler<string>("give_item", GiveItem);
        dialogueRunner.AddFunction<int>("days_since_lamp_lit", DaysSinceLampLit);
        dialogueRunner.AddFunction<string, bool>("has_item", HasItem);
        UpdateInventory();
    }

    public static void PlaySound(string soundName)
    {
        if (soundName == "door_creak")
        {
            Instance.audioSource.PlayOneShot(Instance.doorCreak);
        }
    }

    public static IEnumerator LightLamp()
    {
        var elapsed = 0f;
        while (elapsed < 0.8f)
        {
            elapsed += Time.deltaTime;
            Instance.lampLight.intensity = Mathf.Lerp(0f, 2f, elapsed / 0.8f);
            yield return null;
        }
    }

    public static void GiveItem(string itemName)
    {
        Instance.inventory.Add(itemName);
        Instance.UpdateInventory();
    }

    public static int DaysSinceLampLit()
    {
        return 12;
    }

    public static bool HasItem(string itemName)
    {
        return Instance.inventory.Contains(itemName);
    }

    void UpdateInventory()
    {
        inventoryLabel.text = "Inventory\n" + string.Join("\n", inventory);
    }
}
Lines 9–13 The Dialogue Runner and other parts of the scene, set in the Inspector.
Line 15 The inventory that <<give_item>> adds to and has_item() checks.
Line 17 A property that the static command and function methods can use to access the single Game object’s instance members.
Lines 21–23 Three Commands, each connected to a method on this script.
Lines 24–25 Two Functions. The last type in angle brackets is what each one returns.
Lines 29–35 <<play_sound>>. It starts the sound and returns at once.
Lines 37–46 <<light_lamp>>. A coroutine. The dialogue waits while the lamp fades on over 0.8 seconds.
Lines 48–52 <<give_item>>. Like PlaySound, it isn’t waited for. UpdateInventory redraws the inventory.
Lines 54–57 days_since_lamp_lit() returns a number.
Lines 59–62 has_item() checks the same inventory that <<give_item>> adds to. Its answer changes once the key has been given.
Game.cs. It adds three Commands and two Functions to the Dialogue Runner, and keeps the inventory.

<<move>> isn’t added in Awake. It’s a method marked with [YarnCommand] in the script on Rosa, which Yarn Spinner adds to the Dialogue Runner by itself:

using UnityEngine;
using Yarn.Unity;

public class Character : MonoBehaviour
{
    [YarnCommand("move")]
    public async YarnTask Move(float x, float y)
    {
        var start = transform.position;
        var end = new Vector3(x, y, start.z);
        var elapsed = 0f;
        while (elapsed < 1f)
        {
            elapsed += Time.deltaTime;
            transform.position = Vector3.Lerp(start, end, elapsed);
            await YarnTask.Yield();
        }
    }
}
Line 4 The script goes on the character’s GameObject, which is called Rosa in the scene.
Lines 6–7 A Command on a GameObject, because the method isn’t static. In Yarn, the first argument, Rosa, says which GameObject to call it on. x and y come from the other two arguments, converted to float.
Lines 12–17 Move the GameObject to the new position over one second. The dialogue waits until the move is done, because the method is async and returns a YarnTask.
Character.cs, on Rosa.

What it shows

Every Command in the scene changes something you can see or hear. <<move>> and <<light_lamp>> make the next line wait, and <<play_sound>> and <<give_item>> don’t.

Each Function returns a value the Yarn Script uses: days_since_lamp_lit() puts a number in a line, and has_item() picks which lines Rosa says.

Next step Variables and Storage Reading and changing Yarn variables from C#, and saving and loading them.