Tea exposes a set of global functions that are available in both TwineScript (insideDocumentation 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.
<< >> macros) and in JavaScript blocks (inside <<script>> or the Story JavaScript). These cover deep-copy utilities, random selection, story history queries, persistent storage, dynamic asset loading, and DOM manipulation.
Value utilities
clone(original) → any
Returns a deep copy of the given value. Primitives, generic objects, Array, Date, Map, RegExp, and Set are supported. Custom class instances must implement a .clone() method to be handled correctly — when present, clone() defers to the instance’s own method.
The value to deep-copy.
A deep copy of
original.- TwineScript
- JavaScript
either(...values) → any
Returns a random value selected from the given arguments. Arguments may be individual values, arrays, or a mix — all are concatenated into a single pool before selection. Does not flatten nested arrays; use <Array>.flat() first if needed.
The values to pick from. Arrays are merged into the pool, not selected as units.
- TwineScript
- JavaScript
Persistent storage
These three functions work together to store values that survive story restarts and browser sessions. The metadata store is tied to the specific story and is not a substitute for saves.memorize(key, value)
Saves a key/value pair to the story metadata store so it persists across sessions.
The key under which to store the value.
The value to store.
recall(key [, defaultValue]) → any
Returns the value stored under the given key in the story metadata store. If the key does not exist, returns defaultValue (if provided).
The key to retrieve.
The fallback value to return when the key is absent.
The stored value, or
defaultValue if the key doesn’t exist.forget(key)
Removes the specified key and its associated value from the story metadata store.
The key to remove.
Story history
visited([...passageNames]) → number
Returns the number of times the given passage(s) appear in the story history. When multiple passage names are provided, returns the lowest count among them. Defaults to the current passage if called with no arguments.
Names of passages to count. Defaults to the active passage.
- TwineScript
- JavaScript
hasVisited(...passageNames) → boolean
Returns true if all named passages appear in the story history, false if any are absent. Equivalent to visited(name) > 0 for each name, combined with logical AND.
One or more passage names to check.
lastVisited(...passageNames) → number
Returns the number of turns since the last visit to the given passage, or -1 if it has never been visited. When multiple names are given, returns the lowest count (which may be -1).
One or more passage names.
visitedTags(...tags) → number
Returns the number of passages in the story history that are tagged with all of the given tags.
One or more tag names.
turns() → number
Returns the total number of turns played — i.e., the count of moments in history up to and including the present. Future (rewound) moments are not included.
Passage information
passage() → string
Returns the name of the currently active (present) passage.
previous() → string
Returns the name of the most recent previous passage that is different from the active passage. Returns an empty string if there is no such passage.
tags([...passageNames]) → string[]
Returns a new array containing all the tags of the given passages. Defaults to the tags of the active passage when called without arguments. Passages included via <<include>> or PassageHeader do not count as the active passage.
Passage names whose tags to collect. Defaults to the active passage.
time() → number
Returns the number of milliseconds that have elapsed since the current passage was rendered to the page.
Random numbers
random([min,] max) → number
Returns a pseudo-random integer in the range [min, max] (inclusive). min defaults to 0 when omitted. When the seedable PRNG is enabled via State.prng.init(), results are deterministic.
Lower bound (inclusive). Defaults to
0.Upper bound (inclusive).
randomFloat([min,] max) → number
Returns a pseudo-random floating-point number in the range [min, max) — inclusive minimum, exclusive maximum. min defaults to 0.0 when omitted.
Lower bound (inclusive). Defaults to
0.0.Upper bound (exclusive).
Variable stores
variables() → Object
Returns a direct reference to the active story variables store (equivalent to State.variables). Primarily useful in JavaScript when you want to access $variables programmatically.
temporary() → Object
Returns a direct reference to the current temporary variables store (equivalent to State.temporary). Primarily useful in JavaScript when you need to read or write _temp variables.
DOM utilities
setPageElement(idOrElement, passageName [, defaultText]) → HTMLElement | null
Renders the named passage into the target DOM element, replacing any existing content. When an array of passage names is given, the first match is used. If no passage is found and defaultText is supplied, that text is rendered instead. Returns the element, or null if it cannot be found.
The ID of a DOM element (string) or an element reference.
The name of a passage to render, or an array of names (first found is used).
Fallback text to display if no matching passage is found.
The target element after rendering, or
null on failure.triggerEvent(name [, targets [, options]])
Dispatches a synthetic DOM event with the given name on the specified targets.
The event name to dispatch. Both native and custom event names are supported.
The target(s) to dispatch the event on. Defaults to
document.Options for the dispatched event:
bubbles(boolean) — Whether the event bubbles. Default:true.cancelable(boolean) — Whether the event can be cancelled. Default:true.composed(boolean) — Whether it triggers listeners outside a shadow root. Default:false.detail(any) — Custom data attached toevent.detail. Default:undefined.
Asset loading
importScripts(...urls) → Promise
Dynamically loads external JavaScript files at runtime. Loose URL arguments are loaded concurrently; URLs inside an array are loaded sequentially (useful when script B depends on script A).
The Story JavaScript section is the recommended place to call
importScripts(). Loading is asynchronous — use the returned Promise when you need to ensure scripts are ready before executing dependent code.URLs to load. Pass as separate arguments for concurrent loading, or as an array for sequential loading.
Resolves when all scripts have loaded, or rejects if any script fails.
importStyles(...urls) → Promise
Dynamically loads external CSS stylesheets at runtime. Follows the same concurrent/sequential URL rules as importScripts().
URLs to load. Loose arguments load concurrently; array items load sequentially.
Resolves when all stylesheets have loaded, or rejects if any fails.