bevy_trait

Crates.iobevy_trait
lib.rsbevy_trait
version
sourcesrc
created_at2023-03-01 22:38:50.072074+00
updated_at2025-03-31 01:03:23.319615+00
descriptionMacros for creating Traits in Bevy
homepage
repositoryhttps://github.com/hankjordan/bevy_trait
max_upload_size
id798391
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Hank Jordan (hankjordan)

documentation

README

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 Version Crate Version
0.15 0.3
0.10, 0.11, 0.12, 0.13, 0.14 0.1, 0.2

License

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

Commit count: 5

cargo fmt