Attribute Macro codegen::system[][src]

#[system]
Expand description

Generates legion system setup procedure for.

Consider this example:

struct FooEvent(f32);
struct BarEvent(f32);

struct QuxComp(u32);
struct CorgeComp(u32);

#[derive(Default)]
struct GraultRes(u64);
#[derive(Default)]
struct WaldoRes(u64);

#[codegen::system(Simulate)]
#[read_component(QuxComp)]
#[write_component(CorgeComp)]
fn example(
    world: &mut legion::world::SubWorld,
    #[subscriber] foo_sub: impl Iterator<Item = FooEvent>,
    #[publisher] bar_pub: impl FnMut(BarEvent),
    #[resource] grault_res: &mut GraultRes,
    #[resource] waldo_res: &WaldoRes,
    #[state(0)] local_counter: &mut i32,
) {
    use legion::IntoQuery;

    for (qux, corge) in <(&QuxComp, &mut CorgeComp)>::query().iter_mut(world) {
        corge.0 = qux.0;
    }

    for &FooEvent(float) in foo_sub {
        bar_pub(BarEvent(float));
    }

    grault_res.0 = waldo_res.0;

    *local_counter += 1;
}

fn setup_ecs(setup: codegen::SetupEcs) -> codegen::SetupEcs { setup.uses(example_setup) }

The parameter in the attribute is the SystemClass for the system.

If some of the parameters need to be thread-unsafe, apply the #[thread_local] attribute on the function.