bevy_ecs::schedule

Trait Condition

Source
pub trait Condition<Marker, In = ()>: Condition<Marker, In> {
    // Provided methods
    fn and_then<M, C: Condition<M, In>>(
        self,
        and_then: C,
    ) -> AndThen<Self::System, C::System> { ... }
    fn or_else<M, C: Condition<M, In>>(
        self,
        or_else: C,
    ) -> OrElse<Self::System, C::System> { ... }
}
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.

§Marker type parameter

Condition trait has Marker type parameter, which has no special meaning, but exists to work around the limitation of Rust’s trait system.

Type parameter in return type can be set to <()> by calling IntoSystem::into_system, but usually have to be specified when passing a condition to a function.

fn not_condition<Marker>(a: impl Condition<Marker>) -> impl Condition<()> {
   IntoSystem::into_system(a.map(|x| !x))
}

§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§

Source

fn and_then<M, C: Condition<M, In>>( self, and_then: C, ) -> AndThen<Self::System, C::System>

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.

Source

fn or_else<M, C: Condition<M, In>>( self, or_else: C, ) -> OrElse<Self::System, C::System>

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>)),
);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<Marker, In, F> Condition<Marker, In> for F
where F: Condition<Marker, In>,