Trait valence::prelude::SystemParamFunction
pub trait SystemParamFunction<Marker>:
Send
+ Sync
+ 'static {
type In;
type Out;
type Param: SystemParam;
// Required method
fn run(
&mut self,
input: Self::In,
param_value: <Self::Param as SystemParam>::Item<'_, '_>,
) -> 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::*;
/// 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(In<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, In = A::Out>,
{
// The type of `params` is inferred based on the return of this function above
move |In(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§
type In
type In
The input type to this system. See System::In
.
type Out
type Out
The return type of this system. See System::Out
.
type Param: SystemParam
type Param: SystemParam
The SystemParam
/s used by this system to access the World
.
Required Methods§
fn run(
&mut self,
input: Self::In,
param_value: <Self::Param as SystemParam>::Item<'_, '_>,
) -> Self::Out
fn run( &mut self, input: Self::In, param_value: <Self::Param as SystemParam>::Item<'_, '_>, ) -> Self::Out
Executes this system once. See System::run
or System::run_unsafe
.