Basics: variables, functions and control flow
Variables
let declares a variable you can change, const one you can't. The type comes from the
value, or you write it after a colon.
export function main(): void {
let count = 3; // int
const speed = 2.5; // float
let name: string = "crate";
count += 1;
print(count, speed, name);
}
4 2.5 crate
As in TypeScript:
- A variable can't be used before its declaration (temporal dead zone) or before it has definitely been assigned.
- A
conststruct can't be reassigned, but its fields can be written. varwas removed; useletorconst.- At module level (outside functions) only
constis allowed. Module-levelletis an error, because a module may not hold state between frames.
Functions
function square(x: float): float {
return x * x;
}
// The return type may be left out (except for recursive functions).
function half(x: float) {
return x / 2;
}
function factorial(n: int): int {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
export function main(): void {
print(square(3), half(5), factorial(5));
}
9 2.5 120
- Every parameter needs a type.
- Default parameter values (
x: int = 2) and optional parameters (x?: int) are not supported; both are parse errors. - A function with a non-
voidresult must return on every path:function 'f' may finish without returning a value. exportmakes a function visible to other modules. Exported names become C symbols, so an exported name must be unique across the whole game (duplicate generated C name for 'f').
Control flow
if/else, for, for...of, while, do...while, switch, break and continue
work as in TypeScript. for...in is not supported; use for...of.
export function main(): void {
let total = 0;
for (let i = 0; i < 5; i++) {
if (i == 2) continue;
total += i;
}
const values: int[3] = [10, 20, 30];
for (const v of values) total += v;
let n = 0;
while (n < 3) n++;
do { n--; } while (n > 1);
print(total, n);
}
68 1
Conditions and truthiness
A condition (and an operand of &&, ||, !) may be a bool or any number. A number is
true when it is not zero, as in C. The result of &&, || and ! is always bool.
Vectors, matrices, structs and handles are not conditions.
export function main(): void {
const lives = 3;
const shield = 0.0;
if (lives) print("alive");
if (!shield) print("no shield");
const ok: bool = lives && !shield;
print(ok, lives > 2 ? "many" : "few");
}
alive
no shield
true many
Nullable values are conditions too: if (x) tests that x is present (and, for a nullable
number or bool, that its value is true). See Null safety.
Operators
| Group | Operators |
|---|---|
| Arithmetic | + - * / %, unary -, ++, -- |
| Compound assignment | += -= *= /= %= &= |= ^= <<= >>= >>>= |
| Comparison | == != < > <= >= (strict: no conversions between types) |
| Logical | && || ! |
| Bitwise | & | ^ ~ << >> >>> |
| Other | ? :, ?., ?? |
=== and !== were removed (== is already strict). ** is not supported yet
(exponentiation operators are not supported yet); use pow(x, y).
Built-in functions
These need no import:
| Function | Notes |
|---|---|
min(a, b), max(a, b), clamp(x, lo, hi) |
Numbers or vectors (component-wise) |
abs, floor, ceil, round, sqrt |
round rounds halves up: round(-2.5) is -2 |
pow(x, y), sin, cos, tan, atan2(y, x) |
Radians |
lerp(a, b, t) |
a + (b - a) * t |
dot, cross, length, normalize, distance |
Vectors; see Vectors |
print(...) |
One line, values separated by spaces |
There is no Math object and no console: Math.sin gives unknown name 'Math'.
Comments
// line and /* block */, as in TypeScript.