πŸ€–
Commands and Functions

Defining Commands

You can define your own commands, which allow the scripts you write in Yarn Spinner to control parts of the game that you've built.
In Unity, there are two ways to add new commands to Yarn Spinner: automatically, via the YarnCommand attribute, or manually, using the DialogueRunner's AddCommandHandler method.

The YarnCommand attribute

The YarnCommand attribute lets you expose methods in a MonoBehaviour to Yarn Spinner.
When you add the YarnCommand attribute to a method, you specify what name the command should have in Yarn scripts. You can then use that name as a command.
If the method is static, you call it directly. For example:
1
// Note that we aren't subclassing MonoBehaviour here;
2
// static commands can be on any class.
3
public class FadeCamera {
4
​
5
[YarnCommand("fade_camera")]
6
public static void FadeCamera() {
7
Debug.Log("Fading the camera!");
8
}
9
}
Copied!
If you save this in a file called FadeCamera.cs, you can run this code in your Yarn scripts like this:
1
<<fade_camera>>
2
// will print "Fading the camera!" in the console
Copied!
If the method is not static, you call it with the name of the game object you want the command to run on.
For example:
1
public class CharacterMovement : MonoBehaviour {
2
​
3
[YarnCommand("jump")]
4
public void Jump() {
5
Debug.Log(quot;{name} is jumping!");
6
}
7
}
Copied!
If you save this in a file called CharacterMovement.cs, create a new game object called MyCharacter, and attach the CharacterMovement script to that game object, you can run this code in your Yarn scripts like this:
1
<<jump MyCharacter>>
2
// will print "MyCharacter is jumping!" in the console
Copied!
You can also use methods that take parameters. Yarn Spinner will take the parameters that you provide, and convert them to the appropriate type.
Methods that are used with YarnCommand may take the following kinds of parameters:
Type
Note
string
Passed directly to the function.
int
Parsed as an integer using Convert.ChangeType.
float
Parsed as an integer using Convert.ChangeType.
bool
The strings "true" and "false" are converted to their respective boolean values, true and false. Additionally, the name of the parameter is interpreted as true.
GameObject
Yarn Spinner will search all active scenes for a game object with the given name. If one is found, that game object will be passed as the parameter; otherwise, null will be passed.
Component (or its subclasses)
Yarn Spinner will search all active scenes for a game object with the given name, and then attempt to find a component of the parameter's type on that game object or its children. If one is found, that component will be passed as the parameter; otherwise, null will be passed.
Method parameters may be optional.
For example, consider this method:
1
[YarnCommand("walk")]
2
public void Walk(GameObject destination, bool dancing = false) {
3
var position = destination.transform.position;
4
​
5
// If the second parameter is used in the command,
6
// and it's "true" or "dancing", use a dance
7
// animation
8
if (dancing) {
9
// set animation to a dance
10
} else {
11
// set animation to a regular walk
12
}
13
​
14
// walk the character to 'position'
15
}
Copied!
This command could be called like this:
1
<<walk MyCharacter StageLeft>> // walk to the position of the object named 'StageLeft'
2
​
3
<<walk MyOtherCharacter StageRight dancing>> // walk to StageRight, while dancing
Copied!

Adding commands through code

You can also add new commands directly to a Dialogue Runner, using the AddCommandHandler method.
AddCommandHandler takes two parameters: the name of the command as it should be used in Yarn Spinner, and a method to call when the function is run.
If you want to add a command using AddCommandHandler that takes parameters, you must list the types of those parameters.
For example, to create a command that makes the main camera look at an object, create a new C# script in Unity with the following code:
1
public class CustomCommands : MonoBehaviour {
2
​
3
// Drag and drop your Dialogue Runner into this variable.
4
public DialogueRunner dialogueRunner;
5
​
6
public void Awake() {
7
​
8
// Create a new command called 'camera_look', which looks at a target.
9
// Note how we're listing 'GameObject' as the parameter type.
10
dialogueRunner.AddCommandHandler<GameObject>(
11
"camera_look", // the name of the command
12
CameraLookAtTarget // the method to run
13
);
14
}
15
​
16
// The method that gets called when '<<camera_look>>' is run.
17
private void CameraLookAtTarget(GameObject target) {
18
if (target == null) {
19
debug.Log("Can't find the target!");
20
}
21
// Make the main camera look at this target
22
Camera.main.transform.LookAt(target.transform);
23
}
24
}
Copied!
Add this script to any game object, and it will register the camera_look in the Dialogue Runner you attach.
You can then call this method like this:
1
<<camera_look LeftMarker>> // make the camera look at an object named LeftMarker
Copied!

Making Commands Using Coroutines

​Coroutines can be commands. If you register a command, either using the YarnCommand attribute, or the AddCommandHandler method, and the method you're using it with is a coroutine (that is, it returns IEnumerator, and yields objects like WaitForSeconds), Yarn Spinner will pause execution of your dialogue when the command is called.
For example, here's how you'd write your own custom implementation of <<wait>>. (You don't have to do this in your own games, because <<wait>> is already added for you, but this example shows you how you'd do it yourself.)
1
public class CustomWaitCommand : MonoBehaviour {
2
​
3
[YarnCommand("custom_wait"]
4
IEnumerator CustomWait() {
5
​
6
// Wait for 1 second
7
yield return new WaitForSeconds(1.0);
8
9
// Because this method returns IEnumerator, t's a coroutine.
10
// Yarn Spinner will wait until onComplete is called.
11
}
12
}
Copied!
This new method, once registered with a Dialogue Runner, can be called like this:
1
<<custom_wait>> // Waits for one second, then continues running
Copied!

Defining Functions

​Functions are units of code that Yarn scripts can call to receive a value.
In additon to the built-in functions that come with Yarn Spinner, you can create your own.
To create a function, you use the YarnFunction attribute, or the AddFunction method on a Dialogue Runner. These work very similarly to commands, but with two important distinctions:
  1. 1.
    Functions must return a value.
  2. 2.
    Functions are required to be static.
For example, here's a custom function that adds two numbers together:
1
public class AdderFunction {
2
[YarnFunction("add_numbers")]
3
public static int AddNumbers(int first, int second)
4
{
5
return first + second;
6
}
7
}
Copied!
When this code has been added to your project, you can use it like this:
1
One plus one is {add_numbers(1, 1)}
Copied!
Yarn functions can return the following types of values:
  • string
  • int
  • float
  • bool

Commands, Functions and Assembly Definitions

Yarn Spinner searches your code for methods that have the YarnCommand and YarnFunction attributes when your game first starts up, as well as when a Dialogue Runner is told to run a Yarn Project.
If the Yarn Project's "Search All Assemblies" option is turned on, every assembly definition is searched; if it's turned off, only the assembly definitions specified in the "Assemblies To Search" option is searched. Code that is not in an assembly definition is always included.
By default, Yarn Spinner searches every assembly definition. If you have a large codebase, putting all of the code that contains commands and functions in an assembly definition can reduce the amount of time Yarn Spinner needs to take to find all of the commands and functions.
If "Search All Assemblies" is turned off on your Yarn Projects, and you're seeing errors that mention that a command hasn't been registered, try turning "Search All Assemblies" on. If the error goes away, it means the code for those commands is in an assembly definition that the Yarn Project wasn't using.
Last modified 15d ago