Macro aframe::system_def
source · macro_rules! system_def { ( $(schema: $schema:expr,)? $(init: $init:expr,)? $(pause: $pause:expr,)? $(play: $play:expr,)? $(tick: $tick:expr,)? $(properties: $($fn_name:ident: $func:expr),*)? ) => { ... }; }
Expand description
Top-level macro to define systems. Usage resembles struct creation syntax.
The js!
macro is available for writing inline javascript, and returns a
js_sys::Function object. This macro calls into
on expressions passed into the
fields expecting function, allowing the js!
macro to be used as a catch-all.
Takes the optional fields described in the table below.
field | syntax explanation | description |
---|---|---|
schema | A hashmap containing string keys and ComponentProperty values. Recommend the maplit crate | Describes component properties |
init | JsValue created from a js_sys::Function() | Called on initialization |
pause | JsValue created from a js_sys::Function() | Called when the entity or scene pauses |
play | JsValue created from a js_sys::Function() | Called when the entity or scene resumes |
tick | JsValue created from a js_sys::Function(time, timeDelta) | Called on each tick or frame of the scene’s render loop |
properties | name: JsValue, … | Additional comma-separated functions or data with any valid name may be specified |
All parameteres are optional, although the order must be exactly as shown.
schema
should be a HashMap with string keys and AframeProperty
values. The rest are strings containing javascript code. A js!
macro
is provided to allow inline javascript code to be included in the Rust code
(See the docs for the js!
macro for caveats and limitations). Here’s an
example:
ⓘ
// Example:
let some_system = system_def!
(
schema: hashmap!
{
"some_float" => AframeProperty::float("number", None),
"some_text" => AframeProperty::string("string", Some(Cow::Borrowed("init")))
},
init: js!
(
this.data.some_float = 1.0;
this.data.some_text = "I'm a bit of text";
),
tick: js!
(time, delta =>>
this.data.some_float = this.data.some_float + 1.0;
),
pause: js!(this.data.some_text = "paused!";),
play: js!(this.data.some_text = "playing!";),
properties:
reset_me: js!(this.data.some_float = 0.0;)
);
unsafe
{
some_system.register("system_name");
}