TwineScript is Tea’s scripting language — essentially JavaScript with a layer of syntax sugar to make it more approachable for authors who are not JavaScript developers. You write TwineScript inside macros such asDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/pompom454/tea/llms.txt
Use this file to discover all available pages before exploring further.
<<set>>, <<if>>, <<for>>, and <<print>>, and it gives you full access to variables, expressions, and the complete JavaScript standard library.
Variables
A variable is a named storage location for a value. Tea has two kinds of variables: story variables and temporary variables.- Story variables (sigil:
$) are part of the story history and persist for the lifetime of a playthrough session. They are saved and restored along with saves. - Temporary variables (sigil:
_) do not enter the story history and only exist for the current moment/turn. They are ideal for loop counters and intermediate calculations.
Temporary variables were added in v2.3.0.
Variable name rules
| Position | Allowed characters |
|---|---|
| First character (after sigil) | A–Z, a–z, $, _ |
| Subsequent characters | A–Z, a–z, 0–9, $, _ |
Using variables
Setting variable values
Setting variable values
Use the
<<set>> macro or setter links to assign values to variables.Printing variable values
Printing variable values
Use naked variable markup for simple values, or a print macro for expressions.
Branching on variable values
Branching on variable values
Use
<<if>> and <<switch>> to control story flow based on variable values.Other places to store values
In addition to$ and _ variables, Tea provides two non-persistent storage locations:
setup— an object provided by Tea for storing non-persistent values that should not enter the history (e.g., lookup tables, configuration data).window— the browser’s global object. Properties onwindowbecome auto-globals, but take care not to overwrite any of its many built-in properties.
Supported types
The following types may be safely stored in Tea’s story and temporary variables.Primitive types
- Boolean —
trueandfalse - Number — integers (
42,-24), floating-point (3.14,-17.76), and special values (Infinity,NaN) - String —
"I like pie",'You like pie' - null
- undefined
Object types
ArrayDateMapSet- Generic objects (
{})
Custom object types (classes) can be made compatible with Tea’s serialization by implementing
.clone() and .toJSON() methods. See the Non-generic object types guide for details.Expressions
An expression is any unit of code that yields a value when evaluated.- Arithmetic — yields a number, e.g.,
42or$x + 10 - String — yields a string, e.g.,
"Lulamoon"or$first + " " + $last - Logical — yields a boolean, e.g.,
$health gt 0 - Assignment — causes a value to be stored, e.g.,
$a = 5
Operators
Assignment operators
Use these to store values in variables. TwineScript provides theto keyword as a readable alternative to =.
- TwineScript
- JavaScript style
| Operator | Description | Example |
|---|---|---|
to | Assigns the right-hand value to the left-hand variable | $apples to 6 |
Conditional (comparison) operators
- TwineScript
- JavaScript style
| Operator | Description | Example |
|---|---|---|
is | Strictly equal | $bullets is 6 |
isnot | Strictly not equal | $pie isnot "cherry" |
eq | Equivalent (coercive) — prefer is | $bullets eq 6 |
neq | Not equivalent (coercive) — prefer isnot | $pie neq "cherry" |
gt | Greater than | $cash gt 5 |
gte | Greater than or equal to | $foundStars gte $neededStars |
lt | Less than | $shoeCount lt ($peopleCount * 2) |
lte | Less than or equal to | $level lte 30 |
not | Logical NOT | not $hungry |
and | Logical AND | $age gte 20 and $age lte 30 |
or | Logical OR | $friend is "Sue" or $friend is "Dan" |
def | True if the right side is defined | def $mushrooms |
ndef | True if the right side is not defined | ndef $bottlecaps |