Functions

loreline.Functions (Class)

All built-in functions available to Loreline scripts.

Each public method corresponds to a function that script authors can call directly. Use bindAll() to register every function into a name-to-function map so the interpreter can look them up.

Instance Members

bindAll(target: Map): Void

Registers all built-in functions into the given map, making them callable by name from Loreline scripts.

Name Type
target Map

floor(n: Float): Int

Rounds a number down to the nearest whole number.

floor(3.7) returns 3, floor(-1.2) returns -2.

val = floor(3.7)
You need $val gold coins to enter. // "You need 3 gold coins to enter."
Name Type
n Float
Returns
Int

ceil(n: Float): Int

Rounds a number up to the nearest whole number.

ceil(3.2) returns 4, ceil(-1.8) returns -1.

days = ceil(hours / 24)
The journey takes at least $days days.
Name Type
n Float
Returns
Int

round(n: Float): Int

Rounds a number to the nearest whole number.

round(3.5) returns 4, round(3.4) returns 3.

score = round(raw_score)
Your final score is $score.
Name Type
n Float
Returns
Int

abs(n: Float): Float

Returns the positive version of a number, removing any negative sign.

abs(-5) returns 5, abs(3) returns 3.

diff = abs(your_score - target_score)
You were off by $diff points.
Name Type
n Float
Returns
Float

min(a: Float, b: Float): Float

Returns the smaller of two values.

min(3, 7) returns 3.

damage = min(attack_power, enemy_health)
Name Type
a Float
b Float
Returns
Float

max(a: Float, b: Float): Float

Returns the larger of two values.

max(3, 7) returns 7.

health = max(health - damage, 0)
Name Type
a Float
b Float
Returns
Float

clamp(v: Float, lo: Float, hi: Float): Float

Keeps a value within a given range. If the value is too low, returns the minimum; if too high, returns the maximum; otherwise returns it unchanged.

clamp(10, 0, 5) returns 5, clamp(3, 0, 5) returns 3.

health = clamp(health + healing, 0, max_health)
Name Type
v Float
lo Float
hi Float
Returns
Float

pow(base: Float, exp: Float): Float

Raises a number to the given power.

pow(2, 3) returns 8 (2 x 2 x 2). pow(9, 0.5) returns 3 (square root).

area = pow(side_length, 2)
The room is $area square meters.
Name Type
base Float
exp Float
Returns
Float

random(min: Int, max: Int): Int

Returns a random whole number between min and max, including both ends.

roll = random(1, 6)
You rolled a $roll!
Name Type
min Int
max Int
Returns
Int

chance(n: Int): Bool

Returns true with a 1-in-n probability. Useful for occasional random events.

chance(3) has roughly a 33% chance of being true.

if chance(4)
  You find a rare gem on the ground!
Name Type
n Int
Returns
Bool

seed_random(seed: Float): Dynamic

Sets the random seed so that all future random results follow a predictable sequence. Calling seed_random with the same value always produces the same results for random, chance, random_float, array_pick, and array_shuffle.

seed_random(42)
// From here, the sequence of random values is always the same.
Name Type
seed Float
Returns
Dynamic

random_float(min: Float, max: Float): Float

Returns a random decimal number from min up to (but not including) max.

random_float(0, 1) might return 0.7341....

temperature = round(random_float(15, 30))
It's $temperature degrees outside today.
Name Type
min Float
max Float
Returns
Float

wait(seconds: Float): Async

Pauses the script for the given number of seconds before continuing.

The ground begins to shake...
wait(2)
A massive boulder crashes through the wall!
Name Type
seconds Float
Returns
Async

float_(value: Any): Dynamic

Converts a value to a number. Strings like "3.14" are parsed; true becomes 1, false becomes 0. Returns 0 if conversion fails.

price = float("9.99")
Name Type
value Any
Returns
Dynamic

string_(value: Any): Dynamic

Converts any value to text.

label = string(42)   // "42"
Name Type
value Any
Returns
Dynamic

bool(value: Any): Bool

Converts a value to true or false:

  • Numbers: 0 is false, everything else is true
  • Strings: empty "" is false, non-empty is true
  • Arrays: empty is false, non-empty is true
  • null: false
if bool(item_count)
  You are carrying items.
Name Type
value Any
Returns
Bool

string_length(text: String): Int

Returns the number of characters in a string.

name = "Alice"
Your name has $string_length(name) letters.
Name Type
text String
Returns
Int

string_upper(text: String): String

Converts all letters to uppercase.

string_upper("hello") returns "HELLO".

title = string_upper(player_name)
The crowd chants: $title! $title!
Name Type
text String
Returns
String

string_lower(text: String): String

Converts all letters to lowercase.

string_lower("HELLO") returns "hello".

Name Type
text String
Returns
String

string_contains(text: String, needle: String): Bool

Checks if a string contains a given piece of text.

string_contains("hello world", "world") returns true.

if string_contains(message, "help")
  Someone needs assistance!
Name Type
text String
needle String
Returns
Bool

string_replace(text: String, from: String, to: String): String

Replaces every occurrence of a piece of text with something else.

string_replace("hello world", "world", "there") returns "hello there".

censored = string_replace(message, "darn", "****")
Name Type
text String
from String
to String
Returns
String

string_split(text: String, sep: String): Array<String>

Splits a string into an array of pieces at each occurrence of a separator.

string_split("a,b,c", ",") returns ["a", "b", "c"].

words = string_split(sentence, " ")
The sentence has $length(words) words.
Name Type
text String
sep String
Returns
Array<String>

string_trim(text: String): String

Removes any spaces or whitespace from the beginning and end of a string.

string_trim(" hello ") returns "hello".

Name Type
text String
Returns
String

string_index(text: String, needle: String): Int

Finds where a piece of text first appears inside a string. Returns the position (starting from 0), or -1 if not found.

string_index("hello", "ll") returns 2.

pos = string_index(clue, "treasure")
if pos >= 0
  The clue mentions a treasure!
Name Type
text String
needle String
Returns
Int

string_sub(text: String, start: Any, len: Any): String

Extracts a portion of a string starting at position start (0-based) for length characters.

string_sub("ABCDEF", 0, 3) returns "ABC". string_sub("ABCDEF", 2, 3) returns "CDE".

code = "ABCDEF"
prefix = string_sub(code, 0, 3)
// prefix is "ABC"
Name Type
text String
start Any
len Any
Returns
String

string_starts(text: String, prefix: String): Bool

Checks if a string begins with the given prefix.

string_starts("hello world", "hello") returns true.

if string_starts(name, "Sir")
  You bow before the knight.
Name Type
text String
prefix String
Returns
Bool

string_ends(text: String, suffix: String): Bool

Checks if a string ends with the given suffix.

string_ends("hello world", "world") returns true.

if string_ends(reply, "?")
  It sounds like a question.
Name Type
text String
suffix String
Returns
Bool

string_repeat(text: String, count: Int): String

Repeats the text the given number of times.

string_repeat("ab", 3) returns "ababab".

divider = string_repeat("-", 20)
// divider is "--------------------"
Name Type
text String
count Int
Returns
String

plural(count: Dynamic, singular: String, plural_form: String): String

Returns singular when count is 1, plural_form otherwise. Useful for both noun plurals and verb conjugation. The writer provides both forms, so this works in any language.

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."
Name Type
count Dynamic
singular String
plural_form String
Returns
String

array_length(array: Any): Int

Returns the number of elements in an array.

items = [1, 2, 3]
You carry $array_length(items) items.
Name Type
array Any
Returns
Int

array_add(array: Any, value: Any): Dynamic

Adds an element to the end of an array.

items = ["sword", "shield"]
array_add(items, "potion")
// items is now ["sword", "shield", "potion"]
Name Type
array Any
value Any
Returns
Dynamic

array_pop(array: Any): Dynamic

Removes the last element from an array and returns it. Returns null if the array is empty.

last = array_pop(items)
You drop the $last.
Name Type
array Any
Returns
Dynamic

array_prepend(array: Any, value: Any): Dynamic

Adds an element to the beginning of an array.

queue = ["Bob", "Carol"]
array_prepend(queue, "Alice")
// queue is now ["Alice", "Bob", "Carol"]
Name Type
array Any
value Any
Returns
Dynamic

array_shift(array: Any): Dynamic

Removes the first element from an array and returns it. Returns null if the array is empty.

next_in_line = array_shift(queue)
$next_in_line steps forward.
Name Type
array Any
Returns
Dynamic

array_remove(array: Any, value: Any): Bool

Finds and removes the first occurrence of a value from an array. Returns true if the value was found and removed, false if not found.

array_remove(inventory, "old key")
The old key crumbles to dust.
Name Type
array Any
value Any
Returns
Bool

array_index(array: Any, value: Any): Int

Finds the position of a value in an array (starting from 0). Returns -1 if the value is not in the array.

pos = array_index(suspects, "Butler")
Name Type
array Any
value Any
Returns
Int

array_has(array: Any, value: Any): Bool

Checks if an array contains a given value.

if array_has(inventory, "golden key")
  You unlock the ancient door.
else
  The door won't budge without the right key.
Name Type
array Any
value Any
Returns
Bool

array_sort(array: Any): Dynamic

Sorts the array in place and returns it. Numbers are sorted from smallest to largest; other values are sorted alphabetically.

scores = [30, 10, 20]
array_sort(scores)
// scores is now [10, 20, 30]
Name Type
array Any
Returns
Dynamic

array_reverse(array: Any): Dynamic

Reverses the array in place and returns it.

steps = ["first", "second", "third"]
array_reverse(steps)
// steps is now ["third", "second", "first"]
Name Type
array Any
Returns
Dynamic

array_join(array: Any, sep: String): String

Combines all elements of an array into a single string, placing a separator between each element.

array_join(["a", "b", "c"], ", ") returns "a, b, c".

guests = ["Alice", "Bob", "Carol"]
The guests are: $array_join(guests, ", ").
Name Type
array Any
sep String
Returns
String

array_pick(array: Any): Dynamic

Returns a random element from an array. Returns null if the array is empty. Affected by seed_random.

greetings = ["Hello!", "Hey there!", "Welcome!"]
barista: $array_pick(greetings)
Name Type
array Any
Returns
Dynamic

array_shuffle(array: Any): Dynamic

Shuffles the array in place and returns it. Affected by seed_random.

deck = ["Ace", "King", "Queen", "Jack"]
array_shuffle(deck)
You draw the $deck[0].
Name Type
array Any
Returns
Dynamic

array_copy(array: Any): Dynamic

Returns a shallow copy of the array.

original = [1, 2, 3]
backup = array_copy(original)
array_sort(original)
// original is now [1, 2, 3] sorted, backup is unchanged
Name Type
array Any
Returns
Dynamic

map_length(map: Any): Int

Returns the number of keys in a map.

state
  stats: { strength: 10, agility: 8 }
The map has $map_length(stats) entries.
Name Type
map Any
Returns
Int

map_keys(map: Any): Array<String>

Returns an array containing all the keys of a map.

state
  stats: { strength: 10, agility: 8 }
all_stats = map_keys(stats)
// all_stats is ["strength", "agility"]
Name Type
map Any
Returns
Array<String>

map_has(map: Any, key: String): Bool

Checks if a map contains a given key.

if map_has(inventory_counts, "potion")
  You have potions available.
Name Type
map Any
key String
Returns
Bool

map_get(map: Any, key: String): Dynamic

Gets the value stored under a key in a map. Returns null if the key doesn't exist.

count = map_get(inventory_counts, "arrows")
You have $count arrows left.
Name Type
map Any
key String
Returns
Dynamic

map_set(map: Any, key: String, value: Any): Dynamic

Stores a value under a key in a map. Overwrites any previous value for that key.

map_set(inventory_counts, "arrows", 20)
Name Type
map Any
key String
value Any
Returns
Dynamic

map_remove(map: Any, key: String): Bool

Removes a key and its value from a map. Returns true if the key was found and removed, false otherwise.

map_remove(inventory_counts, "broken_sword")
You discard the broken sword.
Name Type
map Any
key String
Returns
Bool

map_copy(map: Any): Dynamic

Returns a shallow copy of a map.

state
  stats: { strength: 10, agility: 8 }
backup = map_copy(stats)
map_set(stats, "strength", 20)
// stats.strength is 20, backup.strength is still 10
Name Type
map Any
Returns
Dynamic

current_beat(): Dynamic

Returns the name of the beat that is currently running.

beat TavernScene
  where = current_beat()
  // where is "TavernScene"
Returns
Dynamic

has_beat(name: Any): Bool

Checks whether a beat with the given name exists and can be reached from where you are. This includes nested beats defined inside the current beat or any of its parent beats, as well as all top-level beats.

if has_beat("SecretEnding")
  choice
    Try the secret path -> SecretEnding
Name Type
name Any
Returns
Bool

beat_visits(?name: Any): Int

Returns how many times a beat has been entered.

beat_visits() returns the visit count of the current beat. beat_visits("BeatName") or beat_visits(BeatName) returns the visit count of the named beat. BeatName.visits() is also supported via dot notation.

if beat_visits() == 1
  First time here
else
  You've been here before

if beat_visits(Dungeon) >= 3
  You know this place well now
Name Type Default
name Any (optional)
Returns
Int

choices(): Array<Any>

Returns an array of text strings for all enabled choice options evaluated so far (during option condition evaluation) or all enabled options (inside the chosen option's body).

Returns a new array each time (safe to modify in scripts). Returns an empty array outside of a choice context.

choice
  - Ask about menu
  - Ask about specials
  Say something if !choices()
Returns
Array<Any>

choices_disabled(): Array<Any>

Returns an array of text strings for all disabled choice options evaluated so far (during option condition evaluation) or all disabled options (inside the chosen option's body).

Returns a new array each time (safe to modify in scripts). Returns an empty array outside of a choice context.

choice
  Option A if someCondition
  Option B if array_length(choices_disabled()) > 0
Returns
Array<Any>

choices_all(): Array<Any>

Returns an array of text strings for all choice options evaluated so far, in original order, regardless of enabled/disabled state.

Returns a new array each time (safe to modify in scripts). Returns an empty array outside of a choice context.

choice
  Option A
  Option B if array_length(choices_all()) > 0
Returns
Array<Any>

new(interpreter: Interpreter): Void
Name Type
interpreter Interpreter

Private Members

interpreter: Interpreter

rng(): Float
Returns
Float

Metadata

Name Parameters
:hxGen -