Molten

Components, decorators and behaviors

This chapter covers the parts of Cast that exist for the engine. For the concepts, see Components and Behaviors.

@component

@component
export struct Paint {
    @color @group("Appearance") tint: vec4 = vec4(1);
    @readonly @tooltip("Updates per second") @range(-1, 100) updates: int = 20;
    @transient cache: uint64;
    @hidden ready: bool = false;
}

@component (no arguments) marks a struct as a component. The compiler emits its schema: field names, types, offsets, defaults and decorators.

Field decorators

Decorator Effect
@range(min, max) Limits the devtools slider and warns when a file sets a value outside the range. Does not clamp at runtime. Two numeric literals with min <= max
@hidden Hidden in the devtools Inspector only. Still saved, loaded and settable in files
@transient Never saved to files; reset to its default on every code reload. For caches and derived values
@readonly Shown in the Inspector but not editable there. Gameplay can still write it
@color On vec3 / vec4: edited with a color picker (linear RGB / RGBA)
@tooltip("...") Inspector help text
@group("...") Groups fields under a heading in the Inspector

Decorators never run code. Their errors are exact, for example '@range' minimum exceeds maximum, '@hidden' takes no arguments, '@color' requires a vec3 or vec4 field.

What a component field may be

Field type Allowed
bool, int, int32, uint32, int64, uint64, float, float64 Yes
vec2, vec3, vec4, quat Yes
Handle<Object>, Handle<T> (an object type) Yes; default null
int8, int16, uint8, uint16 No: component fields of type uint8 have no M6 schema field type
string, Array<T>, Map<K, V>, class references, fixed arrays Only @transient, with a default or a nullable type: arena-backed and fixed array component fields require @transient
Function values Never

A @transient reference field still follows arena rules: storing a frame allocation in it is rejected (frame allocation escapes into component storage); use new(level) or new(game).

behavior

import { behavior, Transform } from "molten";
import { Spin } from "../components/spin";

export default behavior([Transform, Spin], (objects, dt: float): void => {
    for (const obj of objects) {
        obj.spin.angle += obj.spin.speed * dt;
        const half = obj.spin.angle * 0.5;
        obj.transform.rotation = quat(0, sin(half), 0, cos(half));
    }
});
  • behavior must be imported from "molten", and it must be the file's export default (default export must call 'behavior' imported from 'molten').
  • The list needs at least one component (behavior requires at least one component). Every name in it must be a component; a built-in one you forgot to import gives 'Transform' is not a component.
  • objects has type Query<[Transform, Spin]>. If you annotate it, the list must match (objects annotation must match the behavior component list).
  • obj.<component> is a field per listed component, in lowerCamelCase. Components that aren't listed are not available on obj (type <query object> has no member 'transform').
  • obj.handle is a Handle<Object>; obj.destroy() destroys the object at the end of the tick.
  • Remember that const c = obj.spin; copies the component. Write through obj.spin.x.

Object types in code

Each .object file in the game's objects/ folder is a type, named after the file (grid_node.object is GridNode), with no import. A value of that type has:

  • a field per component listed in the file, plus transform, giving direct access to live storage;
  • a field per part, holding Handle<PartType> | null;
  • .handle and .destroy().
const node = handle.as<GridNode>()?.get();     // GridNode | null
if (node) {
    node.nodeState.kind = 3;
    node.collider.size = vec3(2, 1, 2);
    node.mesh.mesh = loadAsset("models/coal.gltf");
}

Object is the type every object converts to. Handle<Object>.get() returns an Object | null, which only has .handle and .destroy(); reaching a component through it fails (type Object has no member 'coin'). Use .as<T>() first.

Handle<T>.as<U>() requires an authored object type (Handle.as requires an authored object type), checks the exact type and that the object is alive, and returns Handle<U> | null.

View this page's source on GitHub