pub trait SystemParamFunction<Marker>:
Send
+ Sync
+ 'static {
type In: SystemInput;
type Out;
type Param: SystemParam;
// Required method
fn run(
&mut self,
input: <Self::In as SystemInput>::Inner<'_>,
param_value: SystemParamItem<'_, '_, Self::Param>,
) -> Self::Out;
}
Expand description
A trait implemented for all functions that can be used as System
s.
This trait can be useful for making your own systems which accept other systems, sometimes called higher order systems.
This should be used in combination with ParamSet
when calling other systems
within your system.
Using ParamSet
in this case avoids SystemParam
collisions.
§Example
To create something like PipeSystem
, but in entirely safe code.
use std::num::ParseIntError;
use bevy_ecs::prelude::*;
use bevy_ecs::system::StaticSystemInput;
/// Pipe creates a new system which calls `a`, then calls `b` with the output of `a`
pub fn pipe<A, B, AMarker, BMarker>(
mut a: A,
mut b: B,
) -> impl FnMut(StaticSystemInput<A::In>, ParamSet<(A::Param, B::Param)>) -> B::Out
where
// We need A and B to be systems, add those bounds
A: SystemParamFunction<AMarker>,
B: SystemParamFunction<BMarker>,
for<'a> B::In: SystemInput<Inner<'a> = A::Out>,
{
// The type of `params` is inferred based on the return of this function above
move |StaticSystemInput(a_in), mut params| {
let shared = a.run(a_in, params.p0());
b.run(shared, params.p1())
}
}
// Usage example for `pipe`:
fn main() {
let mut world = World::default();
world.insert_resource(Message("42".to_string()));
// pipe the `parse_message_system`'s output into the `filter_system`s input
let mut piped_system = IntoSystem::into_system(pipe(parse_message, filter));
piped_system.initialize(&mut world);
assert_eq!(piped_system.run((), &mut world), Some(42));
}
#[derive(Resource)]
struct Message(String);
fn parse_message(message: Res<Message>) -> Result<usize, ParseIntError> {
message.0.parse::<usize>()
}
fn filter(In(result): In<Result<usize, ParseIntError>>) -> Option<usize> {
result.ok().filter(|&n| n < 100)
}
Required Associated Types§
Sourcetype In: SystemInput
type In: SystemInput
The input type of this system. See System::In
.
Sourcetype Out
type Out
The return type of this system. See System::Out
.
Sourcetype Param: SystemParam
type Param: SystemParam
The SystemParam
/s used by this system to access the World
.
Required Methods§
Sourcefn run(
&mut self,
input: <Self::In as SystemInput>::Inner<'_>,
param_value: SystemParamItem<'_, '_, Self::Param>,
) -> Self::Out
fn run( &mut self, input: <Self::In as SystemInput>::Inner<'_>, param_value: SystemParamItem<'_, '_, Self::Param>, ) -> Self::Out
Executes this system once. See System::run
or System::run_unsafe
.