Skip to main content

script

Macro script 

Source
macro_rules! script {
    (
        $(#[$meta:meta])* $vis:vis struct $name:ident { $($body:tt)* }
        $( $phase:ident ($s:ident, $c:ident) $blk:block )*
    ) => { ... };
    (@phase start $s:ident $c:ident $blk:block) => { ... };
    (@phase update $s:ident $c:ident $blk:block) => { ... };
    (@phase fixed_update $s:ident $c:ident $blk:block) => { ... };
}
Expand description

Declares a whole script in one block: the variables with their defaults (shown in the Inspector and saved in the scene) and the callbacks. No Default, no fields(), no boilerplate.

Callbacks are start, update and fixed_update; each names self and the context. Wrap a variable in Local to keep it out of the Inspector.

use kvas_engine::prelude::*;

script! {
    /// Spins around Y and counts the frames it has been running.
    pub struct Spinner {
        speed: f32 = 90.0,
        axis: Vec3 = Vec3::Y,
        frames: Local<u32> = Local(0),
    }

    start(self, ctx) {
        ctx.log(format!("spinner started at speed {}", self.speed));
    }

    update(self, ctx) {
        ctx.rotate(self.axis * self.speed * ctx.dt());
        *self.frames += 1;
    }
}

let mut spinner = Spinner::default();
assert_eq!(spinner.speed, 90.0);
assert_eq!(spinner.fields().len(), 2, "Local fields are hidden");