pub trait Condition<Marker, In = ()>: Condition<Marker, In> {
// Provided methods
fn and_then<M, C>(
self,
and_then: C
) -> CombinatorSystem<AndThenMarker, Self::System, <C as IntoSystem<In, bool, M>>::System>
where C: Condition<M, In> { ... }
fn or_else<M, C>(
self,
or_else: C
) -> CombinatorSystem<OrElseMarker, Self::System, <C as IntoSystem<In, bool, M>>::System>
where C: Condition<M, In> { ... }
}
Expand description
A system that determines if one or more scheduled systems should run.
Implemented for functions and closures that convert into System<Out=bool>
with read-only parameters.
Examples
A condition that returns true every other time it’s called.
fn every_other_time() -> impl Condition<()> {
IntoSystem::into_system(|mut flag: Local<bool>| {
*flag = !*flag;
*flag
})
}
schedule.add_systems(my_system.run_if(every_other_time()));
A condition that takes a bool as an input and returns it unchanged.
fn identity() -> impl Condition<(), bool> {
IntoSystem::into_system(|In(x)| x)
}
app.add_systems(my_system.run_if(always_true.pipe(identity())));
Provided Methods§
fn and_then<M, C>(
self,
and_then: C
) -> CombinatorSystem<AndThenMarker, Self::System, <C as IntoSystem<In, bool, M>>::System>where
C: Condition<M, In>,
fn and_then<M, C>( self, and_then: C ) -> CombinatorSystem<AndThenMarker, Self::System, <C as IntoSystem<In, bool, M>>::System>where C: Condition<M, In>,
Returns a new run condition that only returns true
if both this one and the passed and_then
return true
.
The returned run condition is short-circuiting, meaning
and_then
will only be invoked if self
returns true
.
Examples
use bevy_ecs::prelude::*;
#[derive(Resource, PartialEq)]
struct R(u32);
app.add_systems(
// The `resource_equals` run condition will panic since we don't initialize `R`,
// just like if we used `Res<R>` in a system.
my_system.run_if(resource_equals(R(0))),
);
Use .and_then()
to avoid checking the condition.
app.add_systems(
// `resource_equals` will only get run if the resource `R` exists.
my_system.run_if(resource_exists::<R>().and_then(resource_equals(R(0)))),
);
Note that in this case, it’s better to just use the run condition resource_exists_and_equals
.
fn or_else<M, C>(
self,
or_else: C
) -> CombinatorSystem<OrElseMarker, Self::System, <C as IntoSystem<In, bool, M>>::System>where
C: Condition<M, In>,
fn or_else<M, C>( self, or_else: C ) -> CombinatorSystem<OrElseMarker, Self::System, <C as IntoSystem<In, bool, M>>::System>where C: Condition<M, In>,
Returns a new run condition that returns true
if either this one or the passed or_else
return true
.
The returned run condition is short-circuiting, meaning
or_else
will only be invoked if self
returns false
.
Examples
use bevy_ecs::prelude::*;
#[derive(Resource, PartialEq)]
struct A(u32);
#[derive(Resource, PartialEq)]
struct B(u32);
app.add_systems(
// Only run the system if either `A` or `B` exist.
my_system.run_if(resource_exists::<A>().or_else(resource_exists::<B>())),
);