Crate bevy_trait

Source
Expand description

§Bevy_trait

Macros for creating Traits in Bevy.

§System

Turn a trait fn into a Bevy system

trait Interactive {
    #[system]
    fn update(damage: f32);
}

/*
// Desugars to
trait Interactive {
    fn update(damage: f32) -> impl System;
}
*/

#[derive(Component)]
struct Health(f32);

#[derive(Component, Copy, Clone)]
struct Cactus;

impl Interactive for Cactus {
    #[system(damage: f32)]
    fn update(
        cacti: Query<&GlobalTransform, With<Cactus>>,
        creatures: Query<(&GlobalTransform, &mut Health), Without<Cactus>>,
    ) {
        // This is a normal Bevy system and accepts SystemParams as such.
        for cactus_gtf in &cacti {
            info!("Damage {:?}", damage); // You can also use params passed into the System builder.

            // ...
        }
    }
}
 
fn run() {
    let system = Cactus::update(42); // This is a System ...
 
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Update, system) // ... that you can add to an App
        .run();
}

§Compatibility

Since bevy_trait does not rely on bevy directly, it is typically compatible across many different versions.

Bevy VersionCrate Version
0.150.3
0.10, 0.11, 0.12, 0.13, 0.140.1, 0.2

§License

bevy_trait is dual-licensed under MIT and Apache-2.0.

Attribute Macros§

boxed_readonly_system
Attribute to use a Trait fn like it’s a boxed ReadOnlySystem.
boxed_readonly_system_with_input
Attribute to use a Trait fn like it’s a boxed ReadOnlySystem, with SystemInput.
boxed_system
Attribute to use a Trait fn like it’s a boxed System.
boxed_system_with_input
Attribute to use a Trait fn like it’s a boxed System, with SystemInput.
readonly_system
Attribute to use a Trait fn like it’s a ReadOnlySystem.
readonly_system_with_input
Attribute to use a Trait fn like it’s a ReadOnlySystem, with SystemInput.
system
Attribute to use a Trait fn like it’s a System.
system_with_input
Attribute to use a Trait fn like it’s a System, with SystemInput.