Built-in Functions
Conversions, math, strings, collections and utilities.
Built-ins are part of the language and need no include. There are 69, including the Custom RPC helpers (HandleRPC, CheckRPC, RPCPack).
Conversion & inspection
knight
len("hello") // 5
str(42) // "42"
int("7") // 7
float("2.5") // 2.5
bool(0) // false
typeof(1.5) // "float"
is_null(null) // true
is_array([1,2]) // true
Math
knight
abs(-5) // 5
min(3, 8) // 3
max(3, 8) // 8
clamp(20, 0, 10) // 10
round(2.6) // 3
floor(2.9) // 2
ceil(2.1) // 3
sqrt(9.0) // 3
pow(2.0, 3.0) // 8
random(1, 6) // 1..6
Strings
knight
upper("ab") // "AB"
lower("AB") // "ab"
trim(" x ") // "x"
substr("hello", 1, 3) // "ell"
contains("abc", "b") // true
starts_with("abc", "a") // true
ends_with("abc", "c") // true
replace("a-b", "-", "+") // "a+b"
split("a,b,c", ",") // ["a","b","c"]
join(["a","b"], "-") // "a-b"
Collections
knight
let a = array();
a = push(a, 10);
a = push(a, 20);
len(a) // 2
pop(a) // 20
at(a, 0) // 10
let m = map("k", 1);
m["z"] = 9;
keys(m) // ["k","z"]
has(m, "k") // true
del(m, "k");
Utilities
knight
format("%s has %d", "CJ", 42) // "CJ has 42"
distance(0.0, 0.0, 0.0, 3.0, 4.0, 0.0) // 5
Complete built-in index
Every built-in function with its signature. Built-ins are resolved before natives, so these names cannot be used for gameplay functions.
Conversions and types 13
| Signature | Description |
|---|---|
len(any value) -> int | Length of a string, array or map. |
str(any value) -> string | Convert a value to a string. |
tostring(any value) -> string | Alias of str. |
int(any value) -> int | Convert to an integer (truncates floats, parses strings). |
float(any value) -> float | Convert to a float. |
bool(any value) -> bool | Convert to a boolean (truthiness). |
typeof(any value) -> string | Type name: int, float, string, bool, array, map, Player, ... |
is_null(any value) -> bool | True when the value is null. |
is_number(any value) -> bool | True for int or float. |
is_string(any value) -> bool | True for string. |
is_bool(any value) -> bool | True for bool. |
is_array(any value) -> bool | True for array. |
is_map(any value) -> bool | True for map. |
Numbers 11
| Signature | Description |
|---|---|
abs(number) -> float | Absolute value. |
round(number) -> int | Round to the nearest integer. |
floor(number) -> int | Round down. |
ceil(number) -> int | Round up. |
sqrt(number) -> float | Square root. |
pow(base, exponent) -> float | Raise base to exponent. |
min(a, b) -> float | Smaller of two numbers. |
max(a, b) -> float | Larger of two numbers. |
clamp(value, low, high) -> float | Limit value to the [low, high] range. |
random(max | min, max) -> int | random() random int; random(n) 0..n-1; random(a,b) inclusive range. |
sign(number) -> int | -1, 0 or 1. |
Strings 17
| Signature | Description |
|---|---|
upper(text) -> string | Uppercase. |
lower(text) -> string | Lowercase. |
trim(text) -> string | Remove leading/trailing whitespace. |
substr(text, start, length) -> string | Substring by rune index. |
contains(text, part) -> bool | Text contains part. |
starts_with(text, prefix) -> bool | Text starts with prefix. |
ends_with(text, suffix) -> bool | Text ends with suffix. |
replace(text, old, new) -> string | Replace every occurrence. |
split(text, separator) -> array | Split into an array of strings. |
join(array, separator) -> string | Join values into one string. |
repeat(text, count) -> string | Repeat text count times. |
char(code) -> string | Character for a code point. |
char_code(text) -> int | Code point of the first character. |
starts_with_any(text, prefixes) -> bool | Text starts with any prefix in the array. |
count(text, part) -> int | Number of non-overlapping occurrences. |
pad_left(text, width, fill) -> string | Pad on the left to width. |
pad_right(text, width, fill) -> string | Pad on the right to width. |
Arrays and maps 17
| Signature | Description |
|---|---|
array(...values) -> array | Create an array. |
map(key, value, ...) -> map | Create a map from alternating keys and values. |
push(array, value) -> array | Append in place and return the array. |
pop(array) -> any | Remove and return the last element. |
shift(array) -> any | Remove and return the first element. |
at(array|map, index|key) -> any | Element by index or key; null when absent. |
keys(map) -> array | Sorted key list. |
values(map) -> array | Value list. |
has(map, key) -> bool | Key exists. |
del(map, key) -> bool | Delete a key in place; true when it existed. |
insert(array, value, index) -> array | New array with value inserted at index. |
slice(array|string, start, end) -> any | Copy of the [start, end) range. |
index_of(array|map, value) -> int | Index/key position or -1. |
reverse(array) -> array | New reversed array. |
sort(array) -> array | New sorted array (numbers numerically, others as text). |
concat(array, array) -> array | New array with both arrays appended. |
unique(array) -> array | New array without duplicates. |
Custom RPC 9
| Signature | Description |
|---|---|
HandleCustomRPC(int rpcID, Function handler, map options) -> bool | Register one handler for an RPC id (incoming/outgoing). |
HandleRPC(int rpcID, Function handler, map options) -> bool | Register a prioritized handler with an optional payload filter. |
CheckRPC(int rpcID, string payload, map options) -> bool | Predicate: does this id and payload match the criteria? |
UnregisterRPC(int rpcID) -> bool | Remove every HandleRPC entry for an id. |
RPCHookCount(int rpcID) -> int | Number of HandleRPC entries for an id. |
SendCustomRPC(int playerID, int rpcID, string payload) -> bool | Send a custom RPC; playerID -1 broadcasts. |
RPCPack(string tag, any value, ...) -> string | Build a little-endian payload (u8/u16/u32/i32/f32/s). |
CustomRPCIds() -> array | Registered custom RPC ids. |
LastCustomRPCStatus() -> string | ok/ignored/too_large/rate_limited/no_client_mod. |
Helpers 2
| Signature | Description |
|---|---|
format(pattern, ...values) -> string | Substitute %s/%d/%f/%v placeholders. |
distance(x1, y1, z1, x2, y2, z2) -> float | 3D distance between two points. |