UNAVI Docs unavi.xyz
Build · Start

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:

FunctionScheduleTypical use
initOnce, before any other exportSetup and initial subscriptions
updateEvery rendered frameVisual interpolation and animation
fixed-update20 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: