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 scene
The Dialogue System comes from . 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>>
===days_since_lamp_lit() returns 12, which is shown in the line.has_item("brass key") returns false, so Rosa hands over the key.true, because the player now has the key.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);
}
}<<give_item>> adds to and has_item() checks.<<play_sound>>. It starts the sound and returns at once.<<light_lamp>>. A coroutine. The dialogue waits while the lamp fades on over 0.8 seconds.<<give_item>>. Like PlaySound, it isn’t waited for. UpdateInventory redraws the inventory.days_since_lamp_lit() returns a number.has_item() checks the same inventory that <<give_item>> adds to. Its answer changes once the key has been given.<<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();
}
}
}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.async and returns a YarnTask.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.