Molten

Step 1: A level and a spinning coin

You will make the smallest complete game: one screen that loads one level, with a ground, a camera and three coins that spin.

The project file

Every game folder has a game.toml at its root. It needs at least first_screen, the screen the game starts on.

# game.toml
name = "CoinYard"
title = "Coin Yard"
first_screen = "Playing"

name is used for the shipped executable and the player's data folder, so keep it to letters, digits, - and _. See game.toml for every key.

A screen

A screen is what the game is doing right now (a menu, playing, paused). Screen names come from the file name: screens/playing.screen is the screen Playing.

// screens/playing.screen
// The screen the game starts on. It loads the "yard" level.
export default screen({
    level: "yard",
});

A level

A level is a place. It is a folder under levels/ with a .level file of the same name for level-wide settings, and .chunk files holding the placed objects.

// levels/yard/yard.level
export default level({
    chunkSize: 64,
    environment: { sunDirection: vec3(-0.4, -1, -0.3), ambient: vec3(0.25, 0.25, 0.3) },
    spawnPoints: [{ id: "start", position: vec3(0, 0, 6) }],
});

The sun direction creates a directional light. The spawn point is used in step 2.

// levels/yard/chunks/0_0.chunk
export default chunk({
    format: "chunk-local-v1",
    coord: [0, 0],
    objects: [
        { id: "ground", type: Ground },
        { id: "camera", type: ViewCamera, position: vec3(0, 6, 12), rotation: euler(-25, 0, 0) },
        { id: "coin-1", type: Coin, position: vec3(0, 1, 0) },
        { id: "coin-2", type: Coin, position: vec3(-4, 1, -3) },
        { id: "coin-3", type: Coin, position: vec3(5, 1, -6), overrides: { coin: { value: 5 } } },
    ],
});
  • The chunk file name 0_0 must match coord: [0, 0].
  • Every placed object has an id that is unique in the level and never changes. Live reload and the editor find objects by it.
  • type names an object type (next section). position, rotation (euler in degrees) and scale place it.
  • overrides changes component values for this one object: coin-3 is worth 5.

Object types

An object type says what a kind of thing is made of: its components and their values. objects/ground.object becomes the type Ground (file name in PascalCase).

// objects/ground.object
// A wide, flat box for everything to stand on.
export default object({
    mesh: { mesh: "models/cube.gltf", color: vec4(0.35, 0.55, 0.3, 1) },
    transform: { position: vec3(0, -0.5, 0), scale: vec3(30, 1, 30) },
});
// objects/view_camera.object
export default object({
    camera: { fovYDegrees: 60 },
});
// objects/coin.object
export default object({
    mesh: { mesh: "models/sphere.gltf", color: vec4(1, 0.8, 0.2, 1), emission: 0.3 },
    transform: { scale: vec3(0.6, 0.6, 0.15) },
    coin: { value: 1 },
});

Each key is a component in lowerCamelCase (mesh is Mesh, coin is the Coin component you write next). Asset paths are relative to assets/. A type's own transform is applied relative to where the object is placed, so the ground sits half a meter down and every coin is flattened into a disc.

A component

A component is a named bundle of data. Write it in Cast as a struct marked @component:

// components/coin.cast
// Data for a coin: how much it is worth and how fast it turns.
@component
export struct Coin {
    @range(1, 100) value: int = 1;
    spinSpeed: float = 2;
    @transient angle: float = 0;
}
  • Fields have types and default values.
  • @range(1, 100) limits the editor's slider and warns if a file sets a value outside it.
  • @transient means "never saved to files; reset on reload". The angle is derived state.

A behavior

A behavior is code that runs every simulation tick on every object that has all of the listed components:

// behaviors/spin.cast
import { behavior, Transform } from "molten";
import { Coin } from "../components/coin";

// Turns every coin around the Y axis.
export default behavior([Transform, Coin], (objects, dt: float): void => {
    for (const obj of objects) {
        obj.coin.angle += obj.coin.spinSpeed * dt;
        const half = obj.coin.angle * 0.5;
        obj.transform.rotation = quat(0, sin(half), 0, cos(half));
    }
});
  • objects holds every object with a Transform and a Coin. Each component is a field named in lowerCamelCase: obj.coin, obj.transform.
  • dt is the fixed tick length in seconds (1/60 by default).
  • sin, cos, quat(...) and the other math functions need no import.

Run it

castc check coinyard
3 objects, 1 screen, 1 level
molten-game --game coinyard

You see a green field and three golden discs turning in place.

A game needs at least one Cast file (or C code in native/) to start. Without one, molten-game stops with Gameplay build requires sources and an output library.

Try live reload

With the game running:

  • In objects/coin.object, change coin: { value: 1 } to coin: { value: 1, spinSpeed: 6 } and save. Every coin speeds up, and the console prints objects/coin.object reloaded (3 objects updated). Their angles are kept: they live in engine memory, not in your code.
  • Change the coin's color in the same file. Every coin updates.
  • In behaviors/spin.cast, change obj.coin.spinSpeed * dt to obj.coin.spinSpeed * dt * 2. The console prints module reloaded in ... ms.
  • Move coin-2 in the chunk file. Only that coin moves.

Changing a default in the component itself (spinSpeed: float = 2 in components/coin.cast) does not change coins that already exist: their stored values are kept across the code reload, and only new coins get the new default. Set per-type values in the .object file instead. See Live reload.

Next: a player you can walk.

View this page's source on GitHub