#bevy-ecs #macro #bevy

macro mevy_ecs

bevy_ecs macros, simplifying child spawning and picking!

1 unstable release

new 0.1.0 Jan 9, 2025

#521 in Procedural macros

Download history 61/week @ 2025-01-03

63 downloads per month
Used in mevy

MIT/Apache

10KB
101 lines

This crate is part of mevy (tl;dr: more neat macros) so take a look! 🦆

A simpler way of spawning

The macro spawn!{..} allows you to spawn hierarchies with this patter:

spawn!{
    // component;
    // .method(..);
    [optional_child_name][
        // component;
        // .method(..);
    ]
}

This macro expects a world variable - which is anything that can spawn things.

  • This means it works with Commands, World, things that impl ChildBuild, ...
fn startup(mut world: Commands){
    spawn!{Camera2d::default()}
}

The 'Child Names' are variables containing the child entity.

  • Inside the macro they can be used anywhere, even 'before' you wrote them
  • If none is provided - one will be generated: e{number}: e0, e1, ...
spawn!{
    Component{ entity: named_child };
    [named_child][
        // components & methods
    ]
}

This is 'token based', which means it preserves LSP greatness.

spawn!{
    // typing . will fire LSPs autocomplete
    .obs // would suggest 'observer'
}

Synergies with mevy

Using mevy_core macro code!{}, you can pair it with code!{} to write Colors and Vals neater:

code!{spawn!{
    BackgroundColor(#ff0000);
    Node{ width:50px, height:10%, margin:[>5px], ..default() }
    [
        // child ui
    ]
}}

Using mevy_ui macro ui!{}, it's a bit like html/css:

spawn!{
    ui!((
        size: 5px;
        box_shadow: 1px 2px 3px 4px #ff0000;
        background: cyan;
    ));
    [inner_box][ui!((
        size: 80%;
        background: green;
    ))]
}

Dependencies

~0.8–1.3MB
~24K SLoC