Built-in Functions

Loreline comes with a collection of built-in functions you can call from expressions, text interpolation, or as standalone statements.

New to Loreline? Start with the Writer's Guide to learn about beats, choices, and state management first. Come back here when you need details about a specific function.

Quick reference

Category Functions
Math floor ceil round abs min max clamp pow
Random random chance seed_random random_float
Timing wait
Type conversion float string bool
String string_upper string_lower string_contains string_replace string_split string_trim string_index string_sub string_starts string_ends string_repeat string_length
Text plural (+ pipe syntax)
Array array_add array_pop array_prepend array_shift array_remove array_index array_has array_sort array_reverse array_join array_pick array_shuffle array_copy array_length
Map map_keys map_has map_get map_set map_remove map_copy map_length
Story state current_beat has_beat

Math

health = clamp(health + healing, 0, max_health)
damage = min(attack_power, enemy_health)

Random

roll = random(1, 6)
You rolled a $roll!

if chance(4)
  You find a rare gem on the ground!

Timing

The ground begins to shake...
wait(2)
A massive boulder crashes through the wall!

Type conversion

String

if string_contains(message, "help")
  Someone needs assistance!

words = string_split(sentence, " ")
The sentence has $array_length(words) words.

if string_starts(name, "Sir")
  You bow before the knight.

All string_ functions also support dot notation: drop the string_ prefix and call the function directly on the value. For example, string_upper(name) can be written as name.upper(), and string_contains(msg, "help") as msg.contains("help"). This also works on literals and can be chained:

title = "hello".upper()
greeting = "  hello world  ".trim().upper()

Text

items = 3
You found $items $plural(items, "coin", "coins").
// "You found 3 coins."

boxes = 1
There $plural(boxes, "is", "are") $boxes $plural(boxes, "box", "boxes") here.
// "There is 1 box here."

There is also a shorthand pipe syntax for the common case. After a numeric $expression, write singular|plural and it will resolve automatically:

items = 3
You found $items coin|coins.
// "You found 3 coins."

boxes = 1
$boxes (box was|boxes were) found.
// "1 box was found."

Use parentheses for multi-word alternatives. Escape with \| if you need a literal pipe character. The pipe syntax only activates after a numeric expression. Otherwise | is kept as-is.

Array

items = ["sword", "shield"]
array_add(items, "potion")

if array_has(inventory, "golden key")
  You unlock the ancient door.

greetings = ["Hello!", "Hey there!", "Welcome!"]
barista: $array_pick(greetings)

All array_ functions also support dot notation. For example, array_add(items, "sword") can be written as items.add("sword"), and array_join(items, ", ") as items.join(", "). This works on literals too:

sorted = [3, 1, 2].sort().join(", ")   // "1, 2, 3"

Map

map_set(inventory_counts, "arrows", 20)
count = map_get(inventory_counts, "arrows")
You have $count arrows left.

All map_ functions also support dot notation. For example, map_get(stats, "hp") can be written as stats.get("hp"):

keys = { name: "Alice", age: 30 }.keys()

Story state

if has_beat("SecretEnding")
  choice
    Try the secret path -> SecretEnding