| Header | Link |
|---|---|
| Purpose | Purpose |
| Context Fields | Context Fields |
| Use Cases | Use Cases |
| API Areas | API Areas |
| Example Shape | Example Shape |
Every script lifecycle hook and method receives one ScriptContext. It is the
single door from your game logic to the engine: read the frame's input, mutate
nodes and physics, load scenes and assets, and emit signals. Instead of holding
global handles, you reach the engine through the context passed into each call,
so hot-reloaded scripts always talk to the live runtime.
Every script lifecycle and method receives one context value.
| Field | Meaning | Use for |
|---|---|---|
ctx.run |
Runtime API window | nodes, scenes, time, window, physics, signals, runtime audio |
ctx.res |
Resource API window | textures, meshes, materials, audio assets, CSV, localization, draw helpers |
ctx.ipt |
Input API window | keys, mouse, gamepads, Joy-Cons, players, action map |
ctx.id |
Current script node ID | self node lookup, state access, node transforms |
- Player controller: read a jump edge with
key_pressed!(ctx.ipt, KeyCode::Space), move the body withctx.run, and step physics each frame. - Scene flow: preload a level with
scene_preload!(ctx.run, ...)inon_init, then swap to it withscene_load!(ctx.run, ...)when the player reaches the exit. - HUD update: pull
delta_time!(ctx.run)and the mouse position fromctx.iptto drive an aim reticle, and load its texture throughctx.res. - Event wiring: connect a button's
pressedsignal inon_all_initand react in amethods!handler that mutates state viawith_state_mut!. - Per-node identity: use
ctx.idto read and write this script's own#[State]block and to look up the node's transform.
ScriptContext borrows live engine access for one callback. Copy or clone needed values out of a node/state closure, let the borrow end, and make the next API call afterward. Do not store the context or nest another ctx.run access inside a closure already borrowing it. Use ctx.id as the owner identity; other targets come from injected references, structure, or deliberate queries.
| Area | Page | Ctx |
|---|---|---|
| Runtime | Runtime API | ctx.run |
| Resource | Resource API | ctx.res |
| Input | Input API | ctx.ipt |
Lifecycle hooks live inside lifecycle!. The macro supplies the impl<API> wrapper, so hooks use API in ScriptContext but do not declare their own generic. Reusable state lives in a #[State] struct, and signal handlers or button callbacks live in methods!.
#[State]
struct PlayerState {
#[default = 0]
coins: i64,
}
lifecycle!({
fn on_update(&self, ctx: &mut ScriptContext<'_, API>) {
let dt = delta_time!(ctx.run);
let jump = key_pressed!(ctx.ipt, KeyCode::Space);
let tex = texture_load!(ctx.res, "res://textures/player.png");
let _ = (dt, jump, tex);
}
});
methods!({
fn on_coin_pickup(&self, ctx: &mut ScriptContext<'_, API>, _coin: NodeID) {
with_state_mut!(ctx.run, PlayerState, ctx.id, |state| state.coins += 1);
}
});