JavaScript API Reference
The complete public API of the Loreline runtime.
New to Loreline in JavaScript? Start with the integration guide. This page is the comprehensive reference.
Quick reference
Loreline
The main public API for Loreline runtime. Provides easy access to the core functionality for parsing and running Loreline scripts.
parse
parse(input, filePath, handleFile, callback)
Parses the given text input and creates an executable Script instance from it.
| Parameter | Type | Description |
|---|---|---|
input |
string |
The Loreline script content as a string (.lor format)
|
filePath |
string |
Optional file path of the input being parsed. If provided, requires handleFile as well.
optional
|
handleFile |
ImportsFileHandler |
Optional file handler to read imports. If that handler is asynchronous, then parse() will return null and callback argument should be used
optional
|
callback |
(script: Script) => void |
If provided, will be called with the resulting script as argument. Mostly useful when reading file imports asynchronously optional |
Returns
Script | null
The parsed script as an AST Script instance (if loaded synchronously)
Throws If the script contains syntax errors or other parsing issues
This is the first step in working with a Loreline script. The returned Script object can then be passed to methods play() or resume().
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
play
play(script, handleDialogue, handleChoice, handleFinish, beatName, options)
Starts playing a Loreline script from the beginning or a specific beat.
| Parameter | Type | Description |
|---|---|---|
script |
Script |
The parsed script (result from parse())
|
handleDialogue |
DialogueHandler |
Function called when dialogue text should be displayed |
handleChoice |
ChoiceHandler |
Function called when player needs to make a choice |
handleFinish |
FinishHandler |
Function called when script execution completes |
beatName |
string |
Optional name of a specific beat to start from (defaults to first beat) optional |
options |
InterpreterOptions |
Additional options optional |
Returns
Interpreter
The interpreter instance that is running the script
This function takes care of initializing the interpreter and starting execution immediately. You'll need to provide handlers for dialogues, choices, and script completion.
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
resume
resume(script, handleDialogue, handleChoice, handleFinish, saveData, beatName, options)
Resumes a previously saved Loreline script from its saved state.
| Parameter | Type | Description |
|---|---|---|
script |
Script |
The parsed script (result from parse())
|
handleDialogue |
DialogueHandler |
Function called when dialogue text should be displayed |
handleChoice |
ChoiceHandler |
Function called when player needs to make a choice |
handleFinish |
FinishHandler |
Function called when script execution completes |
saveData |
SaveData |
The saved game data (typically from interpreter.save())
|
beatName |
string |
Optional beat name to override where to resume from optional |
options |
InterpreterOptions |
Optional options to configure interpreter behavior optional |
Returns
Interpreter
The interpreter instance that is running the script
This allows you to continue a story from the exact point where it was saved, restoring all state variables, choices, and player progress.
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
extractTranslations
extractTranslations(script)
Extracts translations from a parsed translation script.
| Parameter | Type | Description |
|---|---|---|
script |
Script |
The parsed translation script (result from parse() on a .XX.lor file)
|
Returns
Translations
A translations map to pass as InterpreterOptions.translations
Given a translation file parsed with parse(), this returns a translations map that can be passed as options.translations to play() or resume().
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
loadLocale
loadLocale(locale, script, filePath, handleFile, callback)
Loads translations for a specific locale, walking the script's full import tree.
| Parameter | Type | Description |
|---|---|---|
locale |
string |
The locale code (e.g. "fr")
|
script |
Script |
The parsed source script (must have been parsed with a file path, or filePath must be provided)
|
filePath |
string | null |
Optional override for where to look for translation files (defaults to script.filePath). Can be a .lor/.lor.txt file path or a directory.
optional
|
handleFile |
ImportsFileHandler |
File handler used to read translation files optional |
callback |
(translations: Translations) => void |
Called with the merged translations map. Required if handleFile is asynchronous.
optional
|
Returns
Translations | null
The merged translations map (synchronously when handleFile is sync)
For each file involved in the script (root + transitively imported), the corresponding translation file is looked up by inserting .<locale> before the extension (e.g. characters.lor -> characters.fr.lor). Missing translation files are silently skipped. The returned map can be passed as InterpreterOptions.translations to play() or resume().
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
translationFormat
translationFormat(name, enabled)
Enable or disable runtime support for an alternate translation file format.
| Parameter | Type | Description |
|---|---|---|
name |
string |
The format identifier (see above) |
enabled |
boolean |
True to enable the format, false to disable |
Returns
void
By default only .<locale>.lor files are tried by loadLocale. Call this to opt in to additional formats:
"po": GNU gettext PO (.po)"xliff": XLIFF 1.2 / 2.x (.xliff,.xlf)"csv": CSV / TSV (.csv,.tsv)
Unknown names are accepted silently (forward-compat for future formats).
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
lastError
lastError()
Returns the error from the most recent failed parse() or loadLocale() call, or null on success.
Returns
Error | null
In async mode (callback supplied) the callback fires with null on failure and this method tells you what went wrong. In sync mode the call throws, and this is set to the same error so it can be inspected after the catch.
Not thread-safe: read immediately after the call returns.
print
print(script, indent, newline)
Prints a parsed script back into Loreline source code.
| Parameter | Type | Description |
|---|---|---|
script |
Script |
The parsed script (result from parse())
|
indent |
string |
The indentation string to use (defaults to two spaces) optional |
newline |
string |
The newline string to use (defaults to "\n") optional |
Returns
string
The printed source code as a string
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
Interpreter
Runs a parsed script: holds the story state, decides what happens next, and hands control back to your code at every line of dialogue and every choice.
constructor
constructor(script, handleDialogue, handleChoice, handleFinish, options)
Creates a new Loreline script interpreter.
| Parameter | Type | Description |
|---|---|---|
script |
Script |
The parsed script to execute |
handleDialogue |
DialogueHandler |
Function to call when displaying dialogue text |
handleChoice |
ChoiceHandler |
Function to call when presenting choices |
handleFinish |
FinishHandler |
Function to call when execution finishes |
options |
InterpreterOptions |
Additional options optional |
JavaScript TypeScript C# Java Python Haxe
start
start(beatName)
Starts script execution from the beginning or a specific beat.
| Parameter | Type | Description |
|---|---|---|
beatName |
string |
Optional name of the beat to start from. If null, execution starts from the first beat or a beat named "_" if it exists. optional |
Returns
void
Throws RuntimeError If the specified beat doesn't exist or if no beats are found in the script
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
save
save()
Saves the current state of the interpreter. This includes all state variables, character states, and execution stack, allowing execution to be resumed later from the exact same point.
Returns
SaveData
A SaveData object containing the serialized state
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
restore
restore(saveData)
Restores the interpreter state from a SaveData object. This allows resuming execution from a previously saved state.
| Parameter | Type | Description |
|---|---|---|
saveData |
SaveData |
The SaveData object containing the serialized state |
Returns
void
Throws RuntimeError If the save data version is incompatible
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
resume
resume()
Resumes execution after restoring state. This should be called after restore() to continue execution.
Returns
void
getCharacter
getCharacter(name)
Gets a character by name.
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the character to get |
Returns
any
The character's fields or null if the character doesn't exist
getCharacterField
getCharacterField(character, name)
Gets a specific field of a character.
| Parameter | Type | Description |
|---|---|---|
character |
string |
The name of the character |
name |
string |
The name of the field to get |
Returns
any
The field value or null if the character or field doesn't exist
Container values (key-value objects and arrays) are plain JS objects and arrays: field accessors and custom functions exchange idiomatic JS values in both directions.
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
setCharacterField
setCharacterField(character, name, value)
Sets a specific field of a character.
| Parameter | Type | Description |
|---|---|---|
character |
string |
The name of the character |
name |
string |
The name of the field to set |
value |
any |
The value to set |
Returns
void
getStateField
getStateField(name)
Gets a state field by name, resolving from the current scope outward.
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the field to get |
Returns
any
The field value or null if not found
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
setStateField
setStateField(name, value)
Sets a state field by name, resolving from the current scope outward.
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the field to set |
value |
any |
The value to set |
Returns
void
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
getTopLevelStateField
getTopLevelStateField(name)
Gets a field from the top-level state directly.
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the field to get |
Returns
any
The field value or null if not found
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
setTopLevelStateField
setTopLevelStateField(name, value)
Sets a field on the top-level state directly.
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the field to set |
value |
any |
The value to set |
Returns
void
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
currentNode
currentNode()
Returns the current node being executed. During a dialogue callback, this returns the dialogue statement node. During a choice callback, this returns the choice statement node.
Returns
Node | null
The current node or null if no node is being executed
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
currentNodeFilePath
currentNodeFilePath(rootPath)
Returns the file path of the file containing the current node. Resolves through import chains relative to the given root file path.
| Parameter | Type | Description |
|---|---|---|
rootPath |
string |
The file path of the root/main script |
Returns
string
The file path of the file containing the current node, or rootPath if in the root file
JavaScript TypeScript Haxe
Script
Represents the root node of a Loreline script AST.
body
body
Array of top-level declarations in the script.
Returns
Array<Node>
JavaScript TypeScript Haxe
filePath
filePath
The file path this script was parsed from. May be null if the script was parsed without a file path.
Returns
string | null
JavaScript TypeScript Haxe
fromJson
fromJson(json)
Reconstructs a Script from a JSON representation.
| Parameter | Type | Description |
|---|---|---|
json |
any |
The JSON object (as returned by script.toJson())
|
Returns
Script
The reconstructed Script
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
Node
Base class for all AST nodes. Contains position information and basic JSON conversion.
id
id
A unique identifier for this node within the AST, used to distinguish it from other nodes in the script.
Returns
NodeId
JavaScript TypeScript C# Java Haxe
pos
pos
Source code position where this node appears.
Returns
Position
JavaScript TypeScript Haxe
type
type()
Returns the type of this node.
Returns
string
String representation of node type
toJson
toJson()
Converts the node to a JSON representation.
Returns
any
Object containing node type and position
JavaScript TypeScript C# GDScript C++ Java PHP Python Lua Haxe
fromJson
fromJson(json)
Reconstructs a Node from a JSON representation.
| Parameter | Type | Description |
|---|---|---|
json |
any |
The JSON object (as returned by node.toJson())
|
Returns
Node
The reconstructed Node
each
each(handleNode)
Traverses all child nodes of this node.
| Parameter | Type | Description |
|---|---|---|
handleNode |
(node: Node, parent: Node) => void |
Function to call for each child node |
Returns
void
JavaScript TypeScript Haxe
InterpreterOptions
Options used to configure the Loreline interpreter behavior
functions
functions
Optional map of additional functions to make available to the script
Returns
FunctionsMap
strictAccess
strictAccess
Tells whether access is strict or not. If set to true, trying to read or write an undefined variable will throw an error.
Returns
boolean
customCreateFields
customCreateFields()
A custom instantiator to create fields objects.
Returns
(interpreter: Interpreter, type: string, node: Node) => any
JavaScript TypeScript C# Java Haxe
translations
translations
Optional translations map for localization. Built from a parsed translation file using Loreline.extractTranslations().
Returns
Translations
ChoiceOption
Represents a choice option presented to the user.
text
text
The text of the choice option.
Returns
string
tags
tags
Any tags associated with the choice text.
Returns
Array<TextTag>
enabled
enabled
Whether this choice option is currently enabled.
Returns
boolean
TextTag
Represents a tag in text content, which can be used for styling or other purposes.
closing
closing
Whether this is a closing tag.
Returns
boolean
value
value
The value or name of the tag.
Returns
string
offset
offset
The offset in the text where this tag appears.
Returns
number
DialogueHandler
Handler type for text output with callback. This is called when the script needs to display text to the user.
ChoiceHandler
Handler type for choice presentation with callback. This is called when the script needs to present choices to the user.
FinishHandler
Handler type to be called when the execution finishes.
ImportsFileHandler
Handler function for loading file imports
ImportsErrorHandler
Handler function for import errors
FunctionsMap
Map of function names to their implementations
Translations
Opaque type for a translations map. Obtained from Loreline.extractTranslations() and passed to InterpreterOptions.translations.
SaveData
Opaque type for saved game data.
Tokens
Tokens type
Error
Represents an error in the Loreline system.
message
message
The error message describing what went wrong.
Returns
string
JavaScript TypeScript
pos
pos
The position in the source code where the error occurred.
Returns
Position
JavaScript TypeScript
stack
stack
The call stack of this error
Returns
Array<any>
JavaScript TypeScript
toString
toString()
Converts the error to a human-readable string.
Returns
string
Formatted error message with position
JavaScript TypeScript
Position
Represents a position within source code, tracking line number, column, and offset information. Used throughout the compiler to pinpoint locations of tokens, nodes, and error messages.
line
line
The line number in the source code, starting from 1.
Returns
number
JavaScript TypeScript
column
column
The column number in the source code, starting from 1. Represents the character position within the current line.
Returns
number
JavaScript TypeScript
offset
offset
The absolute character offset from the start of the source code. Used for precise positioning and span calculations.
Returns
number
JavaScript TypeScript
length
length
The length of the source text span this position represents. A value of 0 indicates a point position rather than a span.
Returns
number
JavaScript TypeScript
toString
toString()
Converts the position to a human-readable string.
Returns
string
Formatted position string
JavaScript TypeScript
NodeId
Represents a unique identifier for a node within the AST. Uses a structured ID system with section, branch, block, and node components.
toString
toString()
Converts the NodeId to a string representation.
Returns
string
String in the format "section.branch.block.node"
JavaScript TypeScript C# Haxe
toInt64
toInt64()
Converts the NodeId to its Int64 representation.
Returns
Int64
The Int64 value of this NodeId
JavaScript TypeScript Haxe
Int64
An object to store a Int64 from two high and low number values.
high
high
High value of the Int64
Returns
number
JavaScript TypeScript
low
low
Low value of the Int64
Returns
number
JavaScript TypeScript
Fields
Base interface to hold loreline values This interface allows to map loreline object fields to game-specific objects.
lorelineCreate
lorelineCreate(interpreter)
Called when the object has been created from an interpreter
| Parameter | Type | Description |
|---|---|---|
interpreter |
Interpreter |
Returns
void
JavaScript TypeScript C# Haxe
lorelineGet
lorelineGet(interpreter, key)
Get the value associated to the given field key
| Parameter | Type | Description |
|---|---|---|
interpreter |
Interpreter |
|
key |
string |
Returns
any
JavaScript TypeScript C# Haxe
lorelineSet
lorelineSet(interpreter, key, value)
Set the value associated to the given field key
| Parameter | Type | Description |
|---|---|---|
interpreter |
Interpreter |
|
key |
string |
|
value |
any |
Returns
void
JavaScript TypeScript C# Haxe
lorelineExists
lorelineExists(interpreter, key)
Check if a value exists for the given key
| Parameter | Type | Description |
|---|---|---|
interpreter |
Interpreter |
|
key |
string |
Returns
boolean
JavaScript TypeScript C# Haxe
lorelineFields
lorelineFields(interpreter)
Get all the fields of this object
| Parameter | Type | Description |
|---|---|---|
interpreter |
Interpreter |
Returns
string[]
JavaScript TypeScript C# Haxe
Generated from Loreline v0.10.0.