Molten

Structs and classes

Cast has two kinds of user-defined type:

struct class
Kind Value type Reference type
Assignment Copies the whole value Copies the reference
Lives Inline: in a variable, another struct, an array, component storage In an arena (new allocates it)
Identity None Yes; == compares references by default
Can be a component Yes (@component struct) No
Constructor Automatic positional constructor, or your own constructor constructor(...), or an implicit one with no arguments

Neither supports inheritance: class B extends A is an error (inheritance is not supported). Use interfaces.

Structs

struct Item {
    qty: int;
    price: float = 1.5;
    active: bool = true;

    total(): float { return this.qty * this.price; }
}

export function main(): void {
    let a = Item(2);                 // positional constructor; later fields use defaults
    let b = a;                       // a copy
    b.qty++;
    print(a.qty, b.qty, a.total(), Item(4, 2.5, false).active);
}
2 3 3 false
  • The positional constructor takes the fields in order. Fields with defaults may be left off the end; a field without a default must be given (missing constructor argument for field 'qty').
  • A struct with its own constructor uses that instead.
  • Struct methods can read and write this.
  • new is for classes only: new Item(2) is an error ('new' requires a class name).
  • A struct can't contain itself (recursive value type 'A').

Copies are easy to miss. const h = obj.health; copies a component out of an object. Writing h.current = 0 changes the copy only. Write obj.health.current = 0.

Classes

class Counter {
    private value: int = 0;
    readonly label: string;
    static created: int = 0;

    constructor(label: string) {
        this.label = label;
        Counter.created++;
    }

    add(n: int): void { this.value += n; }
    total(): int { return this.value; }
    static count(): int { return Counter.created; }
}

export function main(): void {
    const a = new Counter("a");
    const b = a;                     // same object
    b.add(5);
    const c = new Counter("c");
    print(a.total(), a == b, a == c, a.label, Counter.count());
}
5 true false a 2
  • Fields are public by default. private members are visible only inside the class.
  • readonly fields can be assigned only in the class's own constructor.
  • Field defaults run in declaration order before the constructor. A non-null field without a default must be set on every path through the constructor (constructor must initialize field 'value'). Nullable fields start as null.
  • static members are reached through the class name (Counter.created). Inside a method, this is required for instance members: a bare value is unknown name 'value'.
  • new Counter(...) allocates from the frame arena (freed at the end of the frame). new(game) Counter(...) names another arena. See Memory.

Static fields: the one place for module-wide state

Module-level let is forbidden, but a class's static field is allowed. In a game its storage lives in engine game memory, so it survives code reloads. Gridline keeps its lists of nodes and wires this way:

class Registry {
    static nodes: Array<Handle<Object>> | null;
}

export function nodes(): Array<Handle<Object>> {
    const existing = Registry.nodes;
    if (existing) return existing;
    const created = new(game) Array<Handle<Object>>();
    Registry.nodes = created;
    return created;
}

Note the new(game): a frame allocation stored in a static field would be gone next frame, and the compiler rejects it where it can tell (frame allocation escapes into game storage).

Classes have no layout migration on reload yet: change a class's fields while the game runs and existing instances are not converted. Keep long-lived data in components when you can.

View this page's source on GitHub