KrunkScript Language Reference
Types
KrunkScript is a statically-typed language with the following base types:
| Type | Description | Example |
num | Numeric values (integers and floats) | num x = 42; |
str | String values | str name = "hello"; |
bool | Boolean values | bool active = true; |
obj | Dynamic objects | obj data = { x: 1 }; |
void | No return value (implicit for actions) | action doSomething() {} |
type[] | Array of any type | num[] 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
| Operator | Description |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
** | Exponentiation |
Comparison
| Operator | Description |
== | Equal |
!= | Not equal |
< | Less than |
<= | Less than or equal |
> | Greater than |
>= | Greater than or equal |
Logical
| Operator | Description |
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Bitwise
| Operator | Description |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
>>> | Unsigned right shift |
Assignment
| Operator | Description |
= | 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
| Function | Description | Example |
lengthOf | Get length of array or string | num len = lengthOf myArray; |
notEmpty | Check if array/string is not empty | if (notEmpty myArray) {} |
hasProp | Check if object has property | if (hasProp obj.key) {} |
toStr | Convert value to string | str s = toStr 42; |
toNum | Convert value to number | num n = toNum "42"; |
addTo | Add element to array | addTo myArray newItem; |
remove | Remove element from array/object | remove 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);
# 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
| Type | Example | Description |
| integer | 42, -10, 0 | Whole numbers |
| float | 3.14, -0.5 | Decimal numbers |
| scientific | 1e10, 3e-5 | Scientific notation |
| hex | 0xFF, 0xABCD | Hexadecimal |
Strings
| Type | Example | Description |
| double | "hello world" | Double-quoted strings |
| single | 'hello world' | Single-quoted strings |
| escape | \n, \t, \\ | Escape sequences |
Booleans
true - Boolean true
false - Boolean false