How scripts run
A script is a WebAssembly component attached to a
prim. The host
drives the script through lifecycle callbacks, and the script calls back into
the host through the wired:* APIs.
Lifecycle
Every script exports three lifecycle functions from
wired:script/lifecycle:
| Function | Schedule | Typical use |
|---|---|---|
init | Once, before any other export | Setup and initial subscriptions |
update | Every rendered frame | Visual interpolation and animation |
fixed-update | 20 Hz (fixed timestep) | Simulation, input handling, and messaging |
Calls never overlap. If a callback is still executing when its next turn arrives, that turn is skipped rather than queued.
Subscriptions
Scripts receive incoming input and messages through subscription resources,
such as input-subscription
and message-subscription.
Each subscription buffers up to 64 items, dropping the oldest if it overflows.
Drain it once per fixed-update to process queued events:
impl ScriptBehavior for Script {
fn init() -> anyhow::Result<Self> {
let input = wired::input::device::listen()?;
Ok(Self { input })
}
fn fixed_update(&mut self, _tick: Tick) -> anyhow::Result<()> {
for event in self.input.drain(32) {
println!("got input: {event:#?}");
}
Ok(())
}
}Permissions
A script can only call host APIs allowed by its author's
trust level on the local peer. Because
users can change trust while a script is running, check
wired:script/host.granted before
calling restricted APIs:
if wired::script::host::granted(Permission::InputDevice) {
// Safe to call wired::input::device::listen
}Errors
Host calls that can fail return
wired:core/error. Three errors occur
during normal operation and should be handled explicitly:
permission: the script's author does not have permission for this API on the local peer.rate-limited: the script's rate budget (for calls such ascreate-primoremit) is exhausted. The budget refills over time, so retry on a later tick.limit-reached: the script has hit its resource ceiling. Drop an existing handle before allocating another.