Knight docs
EN ID
GitHub

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

SignatureDescription
len(any value) -> intLength of a string, array or map.
str(any value) -> stringConvert a value to a string.
tostring(any value) -> stringAlias of str.
int(any value) -> intConvert to an integer (truncates floats, parses strings).
float(any value) -> floatConvert to a float.
bool(any value) -> boolConvert to a boolean (truthiness).
typeof(any value) -> stringType name: int, float, string, bool, array, map, Player, ...
is_null(any value) -> boolTrue when the value is null.
is_number(any value) -> boolTrue for int or float.
is_string(any value) -> boolTrue for string.
is_bool(any value) -> boolTrue for bool.
is_array(any value) -> boolTrue for array.
is_map(any value) -> boolTrue for map.

Numbers 11

SignatureDescription
abs(number) -> floatAbsolute value.
round(number) -> intRound to the nearest integer.
floor(number) -> intRound down.
ceil(number) -> intRound up.
sqrt(number) -> floatSquare root.
pow(base, exponent) -> floatRaise base to exponent.
min(a, b) -> floatSmaller of two numbers.
max(a, b) -> floatLarger of two numbers.
clamp(value, low, high) -> floatLimit value to the [low, high] range.
random(max | min, max) -> intrandom() random int; random(n) 0..n-1; random(a,b) inclusive range.
sign(number) -> int-1, 0 or 1.

Strings 17

SignatureDescription
upper(text) -> stringUppercase.
lower(text) -> stringLowercase.
trim(text) -> stringRemove leading/trailing whitespace.
substr(text, start, length) -> stringSubstring by rune index.
contains(text, part) -> boolText contains part.
starts_with(text, prefix) -> boolText starts with prefix.
ends_with(text, suffix) -> boolText ends with suffix.
replace(text, old, new) -> stringReplace every occurrence.
split(text, separator) -> arraySplit into an array of strings.
join(array, separator) -> stringJoin values into one string.
repeat(text, count) -> stringRepeat text count times.
char(code) -> stringCharacter for a code point.
char_code(text) -> intCode point of the first character.
starts_with_any(text, prefixes) -> boolText starts with any prefix in the array.
count(text, part) -> intNumber of non-overlapping occurrences.
pad_left(text, width, fill) -> stringPad on the left to width.
pad_right(text, width, fill) -> stringPad on the right to width.

Arrays and maps 17

SignatureDescription
array(...values) -> arrayCreate an array.
map(key, value, ...) -> mapCreate a map from alternating keys and values.
push(array, value) -> arrayAppend in place and return the array.
pop(array) -> anyRemove and return the last element.
shift(array) -> anyRemove and return the first element.
at(array|map, index|key) -> anyElement by index or key; null when absent.
keys(map) -> arraySorted key list.
values(map) -> arrayValue list.
has(map, key) -> boolKey exists.
del(map, key) -> boolDelete a key in place; true when it existed.
insert(array, value, index) -> arrayNew array with value inserted at index.
slice(array|string, start, end) -> anyCopy of the [start, end) range.
index_of(array|map, value) -> intIndex/key position or -1.
reverse(array) -> arrayNew reversed array.
sort(array) -> arrayNew sorted array (numbers numerically, others as text).
concat(array, array) -> arrayNew array with both arrays appended.
unique(array) -> arrayNew array without duplicates.

Custom RPC 9

SignatureDescription
HandleCustomRPC(int rpcID, Function handler, map options) -> boolRegister one handler for an RPC id (incoming/outgoing).
HandleRPC(int rpcID, Function handler, map options) -> boolRegister a prioritized handler with an optional payload filter.
CheckRPC(int rpcID, string payload, map options) -> boolPredicate: does this id and payload match the criteria?
UnregisterRPC(int rpcID) -> boolRemove every HandleRPC entry for an id.
RPCHookCount(int rpcID) -> intNumber of HandleRPC entries for an id.
SendCustomRPC(int playerID, int rpcID, string payload) -> boolSend a custom RPC; playerID -1 broadcasts.
RPCPack(string tag, any value, ...) -> stringBuild a little-endian payload (u8/u16/u32/i32/f32/s).
CustomRPCIds() -> arrayRegistered custom RPC ids.
LastCustomRPCStatus() -> stringok/ignored/too_large/rate_limited/no_client_mod.

Helpers 2

SignatureDescription
format(pattern, ...values) -> stringSubstitute %s/%d/%f/%v placeholders.
distance(x1, y1, z1, x2, y2, z2) -> float3D distance between two points.