KrunkScript Language Reference

Types

KrunkScript is a statically-typed language with the following base types:

TypeDescriptionExample
numNumeric values (integers and floats)num x = 42;
strString valuesstr name = "hello";
boolBoolean valuesbool active = true;
objDynamic objectsobj data = { x: 1 };
voidNo return value (implicit for actions)action doSomething() {}
type[]Array of any typenum[] scores = num [1, 2, 3];

Arrays

Declare arrays by appending [] to any type:

num[] scores = num [10, 20, 30];
str[] names = str ["Alice", "Bob"];
obj[] players = obj [];

Variables

Declaration

num x = 5;
str name = "hello";
bool flag = true;
obj data = { key: "value" };

Array Declaration

num[] scores = num [10, 20, 30];
str[] names = str ["Alice", "Bob"];
obj[] items = obj [];

Type Casting

Use parentheses to cast types:

(num) obj.property
(str) player.name
(num) obj.value += 5;

Functions (Actions)

Basic Function

action greet(str name) {
    GAME.log("Hello, " + name);
}

Function with Return Value

num action add(num a, num b) {
    return a + b;
}

Public Functions

Public functions can be called from hooks:

public action start() {
    # Entry point
}

Operators

Arithmetic

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulo
**Exponentiation

Comparison

OperatorDescription
==Equal
!=Not equal
<Less than
<=Less than or equal
>Greater than
>=Greater than or equal

Logical

OperatorDescription
&&Logical AND
||Logical OR
!Logical NOT

Bitwise

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift
>>>Unsigned right shift

Assignment

OperatorDescription
=Assignment
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign
++Increment
--Decrement

Control Flow

If/Else

if (x > 5) {
    GAME.log("Greater");
} else if (x == 5) {
    GAME.log("Equal");
} else {
    GAME.log("Less");
}

Ternary Operator

str result = x > 0 ? "positive" : "non-positive";

For Loop

for (num i = 0; i < 10; i++) {
    GAME.log(toStr i);
}

For-In Loop (Objects)

for (str key in myObj) {
    GAME.log(key);
}

While Loop

while (x > 0) {
    x--;
}

Built-in Functions

FunctionDescriptionExample
lengthOfGet length of array or stringnum len = lengthOf myArray;
notEmptyCheck if array/string is not emptyif (notEmpty myArray) {}
hasPropCheck if object has propertyif (hasProp obj.key) {}
toStrConvert value to stringstr s = toStr 42;
toNumConvert value to numbernum n = toNum "42";
addToAdd element to arrayaddTo myArray newItem;
removeRemove element from array/objectremove myArray[0];

Naming Rules

KrunkScript has strict property naming rules:

# naming rules (must begin with character or _)
num test = 0;    # valid
str _test = "hi"; # valid
num 0test = 0;   # invalid
num test0 = 0;   # valid
obj name = {};   # valid

# no special characters allowed

Objects

Object Literal

obj player = {
    name: "John",
    score: 100,
    active: true
};

Property Access

str n = (str) player.name;
num s = (num) player["score"];

Dynamic Properties

str key = "score";
num val = (num) player[key];

Empty Objects

obj other = {};
if (notEmpty other) {
    # this condition would fail
}

Array Operations

Adding & Removing Items

num[] list = num[1, 2, 3, 4, 5];
lengthOf list;     # returns length of list
remove list[0];    # remove first item from list
remove list[i];    # remove specific index from list
addTo list 10;     # add new item to list

Nested Arrays

num[][] nested = num[][num[1], num[1, 2]];

String Manipulation

str testString = "test";
str string2 = testString + "Me";                        # concatenate strings
str string3 = UTILS.toUpper(testString);                 # results in: TEST
str string4 = UTILS.toLower(testString);                 # results in: test
str string5 = UTILS.truncateTxt("123456", 2, true);      # results in: "12"
str string6 = UTILS.truncateTxt("123456", 2, false);     # results in: "12..."
str string7 = UTILS.replaceText("hello there", "the", ""); # results in: "hello re"

# check if string contains a value
if (UTILS.textContains(testString, "test")) {
    # string contains "test"
}

Practical Example

A simple car movement system showing actions, objects, and the game loop:

# create basic car object
obj car = {
    x: 0,
    speed: 0.1,
    fuel: 100
};

# drive action
action drive(num delta) {
    if ((num) car.fuel > 0) {
        car.x = (num) car.x + ((num) car.speed * delta);
        (num) car.fuel -= 0.1 * delta;
    };
}

# game loop (called automatically)
public action update(num delta) {
    drive(delta);
}

# return values
num action addFuel(num amount) {
    GAME.log("Added " + (str) amount);
    return amount;
}
car.fuel = (num) car.fuel + addFuel(10);

Comments

# Single-line comment

# KrunkScript only supports single-line comments
# Use multiple # for multi-line explanations

Reserved Words

bool, num, str, obj, action, public, return, if, else, for, while, in, static, addTo, remove

Plus built-in functions: lengthOf, notEmpty, hasProp, toStr, toNum, addTo, remove

Literals

Numbers

TypeExampleDescription
integer42, -10, 0Whole numbers
float3.14, -0.5Decimal numbers
scientific1e10, 3e-5Scientific notation
hex0xFF, 0xABCDHexadecimal

Strings

TypeExampleDescription
double"hello world"Double-quoted strings
single'hello world'Single-quoted strings
escape\n, \t, \\Escape sequences

Booleans