Yarn Script is a language for writing conversations in games. Here's an updated syntax description based on the provided examples and documentation:
A node is the fundamental building block of Yarn Script. Each node is defined by a title
, followed by a sequence of dialogue, choices, or commands.
- Keyword:
title:
- Format:
title: <NodeName>
- Example:
title: Start --- Hello there! ===
- Start Delimiter:
---
(signals the start of node content) - End Delimiter:
===
(signals the end of a node)
Dialogue is the primary content of nodes. Each line of dialogue is written as plain text.
- Text:
<PlainText>
- Example:
Yarn Spinner is a language for writing conversations in games!
Choices present the player with options that branch the narrative. They are written using arrow symbols and can include nested choices.
- Choice:
-> <ChoiceText>
- Response: Indented text following the choice
- Example:
-> Wow, some options! You got it, pal! -> Can I put text inside options? You sure can! For example, here's some lines inside an option. You can even put options inside OTHER options! -> Like this! Wow! -> Or this! Incredible!
Variables store data that affects dialogue flow or choices. Variables start with $
and can be declared, assigned values, or used in expressions.
- Command:
<<declare $<VariableName> = <Type>>>
- Example:
<<declare $health = Number>> <<declare $playerName = String>> <<declare $playerAlive = Boolean>>
- Format:
{$<VariableName>}
- Example:
My name's {$name}!
Conditionals enable branching based on the value of variables. They are enclosed in <<if>>
, <<else>>
, and <<endif>>
.
- Conditionals:
<<if <Condition>>> ... <<else>> ... <<endif>>
- Example:
<<if $gold > 5>> The '$gold' variable is bigger than 5! <<else>> The '$gold' variable is 5 or less! <<endif>>
Commands allow interaction with the game or the script engine. They are written between double angle brackets.
- Command:
<<command>>
- Example:
<<fade_up 1.0>>
<<jump <NodeName>>>
: Jump to another node.<<set $<VariableName> to <Value>>>
: Assign a value to a variable.<<declare $<VariableName> = <Type>>>
: Declare a variable with a specific type.
Example of declare:
<<declare $health = 50>>
<<set $health to 100>>
Comments are used for annotating the script and are ignored by the interpreter. Yarn supports single-line comments.
- Syntax:
//
- Example:
// Comments start with two slashes, and won't show up in your conversation.
- Syntax:
/* .. .. */
- Example:
/* Comments start with two slashes, and won't show up in your conversation. */
Multi-line Comments: The Comment rule now includes support for multi-line comments, using the /* and */ delimiters. NOTE: It's not originally in the yarn syntax!
The <<jump>>
command can be used to jump to a different node.
- Command:
<<jump <NodeName>>>
- Example:
<<jump OtherNode>>
Variables can be inserted directly into dialogue using curly braces {}
.
- Interpolation:
{$<VariableName>}
- Example:
My name's {$name}!
The overall grammar for Yarn Script can be formalized as follows:
Script ::= Node+ ;
Node ::= "title:" NodeName "---" (Dialogue | Choice | Command | Conditional | Comment)* "===" ;
NodeName ::= [A-Za-z_][A-Za-z0-9_]* ;
Dialogue ::= PlainText ;
Choice ::= "->" ChoiceText (Dialogue | Choice)* ;
Command ::= "<<" CommandText ">>" ;
Conditional ::= "<<if" Condition ">>" Content ("<<else>>" Content)? "<<endif>>" ;
Comment ::= "//" CommentText | "/*" MultiLineComment "*/" ;
MultiLineComment ::= .* ;
PlainText ::= .+ ; // Any printable character or whitespace
ChoiceText ::= .+ ; // Text following the choice arrow
CommandText ::= ("set" | "jump" | "declare") Argument* ; // Command name followed by optional arguments
Argument ::= [A-Za-z_][A-Za-z0-9_]* | String ;
Condition ::= Expression ;
Content ::= (Dialogue | Choice | Command | Conditional | Comment)* ;
Expression ::= Identifier | Number | Boolean | Operator Expression* ;
Identifier ::= [A-Za-z_][A-Za-z0-9_]* ;
Number ::= [0-9]+ | [0-9]*\.[0-9]+ ;
Boolean ::= "true" | "false" ;
Operator ::= "+" | "-" | "*" | "/" | ">" | "<" | ">=" | "<=" | "==" | "!=" | "and" | "or" | "not" ;
String ::= "\"" .* "\"" ;
StringInterpolation ::= "{" "$" Identifier "}" ;
This updated syntax description reflects the structure and features demonstrated in the provided Yarn Script examples.
Florian Fischer ( https://github.com/SiENcE )