Logic and variables
Of course, Yarn Script is actually a full programming language, which means it has support for writing code that stores information in variables.
Yarn has three types of variables, numbers, strings, and booleans:
| Type | Possible Values | Examples |
|---|---|---|
| Number | Any whole or decimal number | 1, 2.5, 3468900, -500 |
| String | Any sequence of letters, numbers and other characters, enclosed in quotes. | “Hello”, “✓”, “A whole sentence.” |
| Boolean | Either the value true or the value false. | true, false |
Variables can be declared, set, and checked.
Declaring variables
A variable is declared using a declare statement. It looks like this:
<<declare $characterName = "Shadowheart">>
This declare statement declares a new variable named $characterName and stores the value "Shadowheart" inside it. When you first declare a variable, that sets its type.
So this variable, $characterName, will always be of type string, because we declared it with a string, "Shadowheart".
Here’s an example of declaring two more variables, one of type number, and one of type boolean:
<<declare $goldAmount = 100>>
<<declare $hasAmulet = false>>
Every variable you use must have a name; in Yarn, all variable names start with a dollar sign ($). As with node titles, variable names must not contain spaces. While they can contain a range of different characters the first character must be a letter. Your variable names will be made up of only letters, numbers and underscores.
Setting variables
Of course, it’s not much use to declare a variable, if you can’t update it later. We call updating the contents of a variable setting. A variable is set using the set statement.
Here’s how we’d update the $characterName variable to a new name:
<<set $characterName to "Karlach">>
Because $characterName was declared as type string, you cannot then set it to a number, or a boolean, so the following will not work:
<<set $characterName to 42>>
A variable must always have a value. There is no concept of null, or an empty variable in Yarn.
Expressions and variables
You can work with the values inside variables. For example, numbers can be multiplied, strings can be added together, and boolean values can have logical operations (like and and or) applied to them.
When values are used together like this, it’s called an expression:
<<set $hamCounts = 2 + 1>>
<<set $numberOfPets = $numberOfPets + 1>>
An expression needs to be a single type. You can’t work with values of different types in a single expression. For example, the following code will not work:
<<set $lemons = "hello" + 1>> // this will not work
Operators
Yarn Spinner supports the following logical operators. Most of these have multiple ways being written:
- Equality:
eqorisor== - Inequality:
neqor! - Greater than:
gtor> - Less than:
ltor< - Less than or equal to:
lteor<= - Greater than or equal to:
gteor>= - Boolean ‘or’’:
oror|| - Boolean ‘xor’:
xoror^ - Boolean ’not’:
notor! - Boolean ‘and’:
andor&&
Maths operators
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Truncating Remainder Division:
% - Brackets:
(to open the brackets and)to close them.
Order of operations
Yarn Spinner follows a fairly standard order of operations, and falls back to using left to right when operators are of equivalent priority.
The order of operations is as follows:
- Brackets
- Boolean Negation
- Multiplication, Division, and Truncating Remainder Division
- Addition, Subtraction
- Less than or equals, Greater than or equals, Less than, Greater than
- Equality, Inequality
- Boolean AND, Boolean OR, Boolean XOR
Checking and using variables
Variables by themselves aren’t much good if they cannot have some sort of impact on your narrative. So, of course, they can!
The most straightforward way to use a variable is to show the contents of it in a line. To do this we refer to the variable by name inside a line, and put it inside braces, like this: {$characterName}.
For example:
title: Start
---
<<declare $characterName = "Player">>
Narrator: Hi there!
Narrator: What is your actual name, anyway?
-> My name is Bruce.
<<set $characterName to "Bruce">>
-> My name is not Bruce.
<<set $characterName to "Notbruce">>
-> My name does not matter...
Narrator: Ah, nice to meet you, {$characterName}!
===
- Put the above script in Try Yarn Spinner, and click Run to play it.
Next, learn about using variables and flow control.