Molten

Components

A component is a named bundle of data attached to an object. Components hold all of a game's state; behaviors are the code that reads and changes them.

Writing a component

A component is a Cast struct marked @component:

// components/health.cast (from samples/farmhouse)
@component
export struct Health {
    @range(0, 500) max: float = 100;
    current: float = 100;
    @hidden regen: float = 0;
}
  • Fields need a type. Fields without a default start at zero (or a null handle).
  • The decorators (@range, @hidden, ...) are metadata for the editor and for saving. They never run code. See Components, decorators and behaviors.
  • Every component gets a schema (field names, types, offsets, defaults) that the engine, devtools and the file loader read at runtime.

Field types a component can hold

Allowed Notes
bool, int/int32, uint32, int64, uint64, float, float64 Saved and editable
vec2, vec3, vec4, quat Saved and editable
Handle<Object>, Handle<T> for an object type Saved as a handle; defaults to null
Asset handles (built-in components only, such as Mesh.mesh) Written as a path in files
string, Array<T>, Map<K, V>, class references, fixed arrays Only as @transient fields, which are not saved and reset on reload

Not allowed: int8, int16, uint8, uint16 (no schema field type yet) and function values.

Built-in components

The engine defines these; you use them like your own:

Component Key in files Purpose
Transform transform Position, rotation, scale. Every object has one
Mesh mesh What the object looks like
Camera camera The scene is drawn from the first camera
Light light A directional light
Parent (added for parts) Attaches an object to another
AudioSource audioSource A sound that belongs to the object
RigidBody rigidBody Makes a collider move
Collider collider A physics shape
CharacterController characterController A walking capsule

Fields and defaults: Built-in components.

Component names in files and code

Where Spelling Example
Declaration PascalCase struct name struct NavAgent
.object / overrides key lowerCamelCase navAgent: { speed: 3 }
Behavior and object fields lowerCamelCase obj.navAgent.speed
Devtools and the dev link schema name NavAgent

The lowercasing covers a leading acronym: HTTPThing becomes httpThing.

Changing a component while the game runs

Edit the struct and save. The engine migrates every existing instance by field name: matching fields keep their values, new fields get their default, removed fields are dropped, and a changed number type keeps values it can represent exactly (otherwise it falls back to the default with a warning). Existing objects keep their values even if you change a field's default; only new objects use the new default. See Live reload.

View this page's source on GitHub