Molten

castc output and the gameplay module (M6)

The contract between castc (libcast, the compiler) and the engine's module builder. The language itself is defined by docs/DESIGN.md "Language spec" plus the test suite under tests/.

Command

castc build GAME_DIR OUT_DIR [--dev | --release] [--no-entry]
  • Compiles every .cast file under GAME_DIR (in any folder except native/, assets/ and folders starting with .), following imports.
  • On success, exits 0 and leaves exactly these files in OUT_DIR, rewriting only the files whose content changed so incremental tools see real changes:
    • one .c file per .cast module, named after its path: behaviors/health.cast -> behaviors__health.c;
    • game_generated.h: the C declarations of every exported function, struct and component, for hand-written C;
    • schema.c: one MtComponentDesc per @component struct, built from the struct layout and decorators;
    • cast_module.c: defines void molten_cast_on_load(const MtApi* api), which registers every component, then every behavior;
    • cast_files.txt: the list of .c files to compile, one per line.
  • Unless --no-entry is given, cast_module.c also defines the module entry (MOLTEN_MODULE) with on_load = molten_cast_on_load. The builder passes --no-entry when a file in native/ defines MOLTEN_MODULE; that hand-written on_load must then call molten_cast_on_load(api) itself (declared in game_generated.h).
  • On error, prints the diagnostics as file:line:col: error: message (paths relative to GAME_DIR), exits 1, and leaves OUT_DIR untouched, so the running module stays as it is.
  • --dev adds the dev checks: integer divide-by-zero logs through api->log with the .cast file and line. --release omits them.

Generated C

  • Hand-written C calls an exported Cast function using its declaration in game_generated.h. For example, export function select(...) is called as cast_export_select(...). The cast_export_ prefix also applies to exported constant accessors and is reserved for generated declarations. Use the spelling in the header for names that require escaping, such as names containing $. The standalone Cast main is invoked by a generated C entry point. A declare function uses the original C symbol name.
  • C11 that compiles warning-free with TinyCC, gcc and clang (-Wall -Wextra) and MSVC (/W4). The output uses <molten.h> and the standard headers <stdint.h>, <stdbool.h>, <math.h>, <string.h>, <stdlib.h> and <stdio.h>.
  • #line directives point back to the .cast source.
  • No statics hold state between frames. Component ids are looked up with api->find_component during the call that uses them, or passed in by position: a behavior's columns are in the order of its component list.
  • A behavior from behaviors/health.cast is registered as "behaviors/health" (its path without the extension) and lowers to one MtBehaviorFn that loops over chunk->count rows. obj.destroy() becomes api->destroy(chunk->handles[i]).
  • Built-in component types map to the engine's: Transform -> MtTransform (MT_TRANSFORM), and likewise Mesh, Camera, Light. vec2/vec3/dvec3/vec4/quat are layout-compatible with MtVec2/MtVec3/MtVec4/MtQuat; mat3/mat4 are column-major float arrays.
  • Schema field types: bool -> MT_FIELD_BOOL, int/int32 -> INT32, uint32 -> UINT32, int64 / uint64 likewise, float -> FLOAT, float64 -> FLOAT64, vec2/vec3/dvec3/vec4/quat -> the vector types. int8, int16, uint8 and uint16 fields in components are a compile error in M6, since the schema has no such field types yet.

The "molten" module (M6)

import { ... } from "molten" provides behavior, Transform, Mesh, Camera and Light. Built-in functions (min, max, ..., print) need no import. The rest of the engine API also exposes spawning, screens and input. Cast screen hooks use module-qualified C symbols. Their adapters register the module path through the engine's screen API.

Module build (engine)

When a game folder has any .cast file, the module builder runs castc build with --dev in Debug or --release in Release. It writes the output into <work dir>/cast/, then compiles those files plus every native/**/*.c with TinyCC into the gameplay module. Games without .cast files keep building with the platform compiler, as before. Live reload watches .cast files as well as native/.

molten build --release consumes the same C output with release checks disabled. It compiles the generated and native gameplay sources with clang and links the molten_module entry into the shipped executable. See Shipping a Molten game.

View this page's source on GitHub