Flow Control
Learn how to use logic and flow control in your Yarn Spinner Scripts.
So far, you've learned how to use Nodes and Lines, Options, the Jump Command, the Detour Command, and Variables to write Yarn Spinner Scripts. Because it's actually a full programming language of its own, Yarn Spinner also supports flow control.
Flow control takes several forms, including if statements, and conditional options.
if
statements
if
statementsIn addition to storing information, variables are useful for controlling what's lines of dialogue are presented to the player. To do this, you can use if
statements.
An if
statement allows you to control whether a collection of content is shown or not. When you write an if
statement, you provide an expression, which is checked; if that expression evaluates to a true
value, then all of the content in between the <<if>>
and <<endif>>
statements are run.
For example, consider the following Yarn Spinner Script:
This script will:
set a variable,
$gold_amount
, to 5;show the line
Player: I'd like to buy a pie!
use an if statement to see if
$gold_amount
is less than10
, and if that is the case, which it will be in this example, show the lineBaker: Well, you can't afford one!
elseif
and else
elseif
and else
You can use the elseif
and else
statements to handle different situations in an if
statement.
An elseif
statement has an expression that gets checked if the if
statement, or any previous elseif
statements, don't run.
An else
statement doesn't have an expression, and runs if the if
and any elseif
don't run.
For example, consider the following Yarn Spinner Script:
This script will:
set a variable,
$gold_amount
, to 5;show the line
Player: I'd like to buy a pie!
use an if statement to see if
$gold_amount
is less than10
, and if that is the case, show the lineBaker: Well, you can't afford one!
otherwise use a
elseif
statement to see if$gold_amount
is less than15
, and if that is the case, show the lineBaker: You can almost afford one!
otherwise use a
else
statement to provide a fallback, which will show the lineBaker: You can afford a pie!
end the
if
statement block with anendif
statement.
In this example:
there are two variables,
$gold_amount
for tracking the player's currency, and$reputation
for their reputation in townan
if
statement first checks if their$gold_amount
is greater than or equal to5
and whether their reputation is greater than or equal to8
, if both of these are true, then the lineMerchant: You're rich enough and popular enough for me to serve you!
is shownotherwise the elseif statement checks if their $gold_amount is greater than or equal to 10 (making them very rich), or their $reputation is greater than or equal to 10 (making them very well regarded), and if either of these are true the line
Merchant: I wouldn't normally, but I'll serve you!
is shown
if neither set of conditions is true, then the else statement means the line
Merchant: You're neither rich enough nor important enough for me to serve!
will be shown
Conditional Options
When presenting options to the player using the ->
syntax, you may want to make some options not available. You can do this by adding a condition to the option, making it a conditional option.
For example, if you have a variable that tracks your player's reputation points, called $reputation
, you might want to make certain options only available if the value of $reputation
is high enough.
Conditions on options are done by adding an if
statement to the end of the option. They look like this:
When Yarn Spinner runs this collection of options, it will check the expression inside the if
statement. If the expression is false
, then the option will be marked as unavailable.
Last updated
Was this helpful?