bevy-trait-resource

A way to get resources that implements a specific trait.

Note

This crate is highly experimental.

The code is derived and heavily relies on previous work done in bevy-trait-query.

Use

```rust use bevy::prelude::*; use bevytraitresource::{trait_resource, TraitResourceExt};

[trait_resource]

pub trait IncrementTrait { fn value(&self) -> i32; fn increment(&mut self); }

[derive(Resource, Default)]

struct NumberValueResource { value: i32, }

impl IncrementTrait for NumberValueResource { // Trait implementation... }

pub fn incrementvaluesystem(world: &mut World) { for resopt in world.getresourcestraitmut::() { if let Some(res) = res_opt { res.increment(); } } }

struct SomePlugin;

impl Plugin for SomePlugin { fn build(&self, app: &mut App) { app.initresourceas::(); app.addsystem(incrementvalue_system); } }

You can also register a resource to multiple traits. rust impl Plugin for SomePlugin { fn build(&self, app: &mut App) { app.init_resource::();

    app.register_resource_as::<dyn IncrementTrait, NumberValueResource>()
       .register_resource_as::<dyn SomeOtherTrait, NumberValueResource>();
}

} Unregistering. rust app.unregisterresourcefrom_trait::(); ```