pub trait EntitySystem:
Send
+ Sync
+ 'static {
type Data: QueryData;
type Filter: QueryFilter;
type Param: SystemParam;
type In;
type Out;
// Required method
fn run(
&mut self,
input: Self::In,
data_value: QueryItem<'_, Self::Data>,
param_value: SystemParamItem<'_, '_, Self::Param>,
) -> Self::Out;
}Expand description
Trait implemented for all functions that can be used as Systems
and operate on a single Entity.
Such system can only be run for entities that match it’s Data and Filter
Every entity system that is function that has Data
as a first (or second after In) parameter
#[derive(Component)]
struct Count(i32);
#[derive(Component)]
struct MyMarkerComponent;
fn my_entity_system(
mut data: Data<&mut Count, With<MyMarkerComponent>>,
mut commands: Commands
) {
data.item.0 += 1;
commands.spawn(Count(10));
}
fn my_entity_system_with_input(input: In<i32>, mut data: Data<&mut Count>) {
data.item.0 += *input;
}
§Warning
Currently there is a bug, individual entity systems don’t store their state.
State is being shared across all entities that are being iterated by the system that resulted from into_system call.
That means that Local, EntityReader and other SystemParam’s, that
rely on it’s state property to be preserved between system runs, won’t work correctly.
It is a huge footgun, so the work on fixing it is being done.
After fix, the API will change, user would need to explicitly specify the entities this system is allowed to run on. That means, you most probably would need to be able to mutate systems at runtime - something that bevy currently doesn’t support. I also work on a crate that will be able to provide you with such functionality - it is not published yet.
§Custom implementation
If you have a custom implementation of the trait, it’s highly recommended to also
derive IntoSystem trait
§Note
Since IntoSystem is defined in bevy_ecs crate, it’s impossible to implement it for the rust functions.
Use into_system or
into_system_with_output
instead or convert to one of the type that implements such trait.
Required Associated Types§
Sourcetype Filter: QueryFilter
type Filter: QueryFilter
Filtering of the entities that are allowed to be run on by this system
Sourcetype Param: SystemParam
type Param: SystemParam
SystemParam’s of the system