Modules: import and export
Every .cast file is a module. Its top level may contain imports, functions, structs,
classes, interfaces, type aliases, const declarations, declare declarations and one
export default behavior(...). Module-level let is not allowed.
// components/shared.cast
export const SCALE: float = 2.5;
export struct Sample { value: float; }
export function triple(a: float): float { return a * 3; }
// behaviors/use.cast
import { SCALE, triple, Sample } from "../components/shared";
Rules
- Import paths are relative (
"./hud","../components/health") and leave out.cast. - Only exported names can be imported:
'../fixtures/shared' does not export 'Missing'. - Imports can't form a cycle (
cyclic import of 'fixtures/cycle_a.cast'). - Exported functions become C symbols named after the function, so exported function
names must be unique across the whole game (
duplicate generated C name for 'f'). Non-exported functions don't have this problem. - Object types from
objects/are available everywhere without an import. - Which files are compiled: every
.castfile under the game folder except innative/,assets/and folders whose name starts with..
The "molten" module
Engine functionality comes from the built-in module "molten":
import { behavior, Transform, keyDown, spawnObject, uiSetText } from "molten";
It provides behavior, the built-in components (Transform, Mesh, Camera, Light, Parent,
AudioSource, RigidBody, Collider, CharacterController), the physics result types
PhysicsHit and PhysicsContact, and the engine functions. The full list with signatures
is in the Engine API reference.
The math functions and print are built in and need no import.
UI modules
Event handlers named in a .ui file are exports of a Cast module, imported in the .ui
file with a relative path:
import { play, quit } from "./actions";
In the gameplay module they are registered as ui/actions#play (module path plus # and
the export name).