Skip to main content

SystemCondition

Trait SystemCondition 

Source
pub trait SystemCondition<Marker, In: SystemInput = ()>: IntoSystem<In, bool, Marker, System: ReadOnlySystem> {
Show 14 methods // Provided methods fn and_then<M, C: SystemCondition<M, In>>( self, then_run: C, ) -> AndThen<Self::System, C::System> { ... } fn and_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> AndEager<Self::System, C::System> { ... } fn and<M, C: SystemCondition<M, In>>( self, then_run: C, ) -> AndThen<Self::System, C::System> { ... } fn nand_then<M, C: SystemCondition<M, In>>( self, then_run: C, ) -> NandThen<Self::System, C::System> { ... } fn nand_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> NandEager<Self::System, C::System> { ... } fn nand<M, C: SystemCondition<M, In>>( self, nand: C, ) -> NandThen<Self::System, C::System> { ... } fn nor_else<M, C: SystemCondition<M, In>>( self, else_run: C, ) -> NorElse<Self::System, C::System> { ... } fn nor_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> NorEager<Self::System, C::System> { ... } fn nor<M, C: SystemCondition<M, In>>( self, else_run: C, ) -> NorElse<Self::System, C::System> { ... } fn or_else<M, C: SystemCondition<M, In>>( self, else_run: C, ) -> OrElse<Self::System, C::System> { ... } fn or_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> OrEager<Self::System, C::System> { ... } fn or<M, C: SystemCondition<M, In>>( self, else_run: C, ) -> OrElse<Self::System, C::System> { ... } fn xnor<M, C: SystemCondition<M, In>>( self, other: C, ) -> Xnor<Self::System, C::System> { ... } fn xor<M, C: SystemCondition<M, In>>( self, other: C, ) -> Xor<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

SystemCondition 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 SystemCondition<Marker>) -> impl SystemCondition<()> {
   IntoSystem::into_system(a.map(|x| !x))
}

§Examples

A condition that returns true every other time it’s called.

fn every_other_time() -> impl SystemCondition<()> {
    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 SystemCondition<(), In<bool>> {
    IntoSystem::into_system(|In(x): In<bool>| x)
}

app.add_systems(my_system.run_if(always_true.pipe(identity())));

Provided Methods§

Source

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

Returns a new run condition that only returns true if both this one and the passed then_run return true.

The returned run condition is short-circuiting, meaning then_run will only be invoked if self returns true.

Short-circuiting may not be desired in all cases; when utilizing change detection, the then_run condition will react to changes since the last time that self returned true, which may introduce subtle inconsistencies if short-circuiting was not intended. Similar issues may arise for run conditions that rely on internal state, such as those using Local<T> parameters or MessageReader<T>, as they may not be updated every time the combined condition is evaluated.

See also and_eager, which always evaluates both conditions.

§Examples
use bevy_ecs::prelude::*;

#[derive(Resource, PartialEq)]
struct R(u32);

schedule.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.

schedule.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 specific case, it’s better to just use the run condition resource_exists_and_equals.

Source

fn and_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> AndEager<Self::System, C::System>

Returns a new run condition that only returns true if both this one and the passed then_run return true.

The returned run condition is eagerly evaluated, meaning it will always execute both run conditions in order.

When applied directly to a system using run_if, the use of this combinator is behaviorally identical to simply calling run_if multiple times. However, .and_eager may be more efficient, as it does not erase the types of the inner conditions when evaluating them, which may allow for compiler optimizations that are not possible with separate calls to run_if.

See also and_then, which short-circuits if self returns false.

§Examples
schedule.add_systems(
    // both conditions will execute, even though the first one returned false
    my_system.run_if(returns_false.and_eager(returns_true)),
);
Source

fn and<M, C: SystemCondition<M, In>>( self, then_run: C, ) -> AndThen<Self::System, C::System>

👎Deprecated since 0.19.0:

use .and_then(...) instead, or .and_eager(...) to evaluate the conditions eagerly

Returns a new run condition that only returns true if both this one and the passed then_run return true.

Source

fn nand_then<M, C: SystemCondition<M, In>>( self, then_run: C, ) -> NandThen<Self::System, C::System>

Returns a new run condition that only returns false if both this one and the passed then_run return true.

The returned run condition is short-circuiting, meaning then_run will only be invoked if self returns true.

Short-circuiting may not be desired in all cases; when utilizing change detection, the then_run condition will react to changes since the last time that self returned true, which may introduce subtle inconsistencies if short-circuiting was not intended. Similar issues may arise for run conditions that rely on internal state, such as those using Local<T> parameters or MessageReader<T>, as they may not be updated every time the combined condition is evaluated.

See also nand_eager, which always evaluates both conditions.

§Examples
schedule.add_systems(
    // The game_over_credits system will only execute if either the `in_state(PlayerState::Alive)`
    // run condition or `in_state(EnemyState::Alive)` run condition evaluates to `false`.
    game_over_credits.run_if(
        in_state(PlayerState::Alive).nand_then(in_state(EnemyState::Alive)),
    ),
);

Equivalent logic can be achieved by using not in concert with and_then:

schedule.add_systems(
    game_over_credits.run_if(
        not(in_state(PlayerState::Alive).and_then(in_state(EnemyState::Alive))),
    ),
);
Source

fn nand_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> NandEager<Self::System, C::System>

Returns a new run condition that only returns false if both this one and the passed then_run return true.

The returned run condition is eagerly evaluated, meaning it will always execute both run conditions in order.

See also nand_then, which short-circuits if self returns false.

Source

fn nand<M, C: SystemCondition<M, In>>( self, nand: C, ) -> NandThen<Self::System, C::System>

👎Deprecated since 0.19.0:

use .nand_then(...) instead, or .nand_eager(…)` to evaluate the conditions eagerly

Returns a new run condition that only returns false if both this one and the passed then_run return true.

Source

fn nor_else<M, C: SystemCondition<M, In>>( self, else_run: C, ) -> NorElse<Self::System, C::System>

Returns a new run condition that only returns true if both this one and the passed else_run return false.

The returned run condition is short-circuiting, meaning else_run will only be invoked if self returns true.

Short-circuiting may not be desired in all cases; when utilizing change detection, the else_run condition will react to changes since the last time that self returned true, which may introduce subtle inconsistencies if short-circuiting was not intended. Similar issues may arise for run conditions that rely on internal state, such as those using Local<T> parameters or MessageReader<T>, as they may not be updated every time the combined condition is evaluated.

See also nor_eager, which always evaluates both conditions.

§Examples
use bevy::prelude::*;

#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum WeatherState {
    Sunny,
    Cloudy,
}

#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum SoilState {
    Fertilized,
    NotFertilized,
}

app.add_systems(
    // The slow_plant_growth system will only execute if both the `in_state(WeatherState::Sunny)`
    // run condition and `in_state(SoilState::Fertilized)` run condition evaluate to `false`.
    slow_plant_growth.run_if(
        in_state(WeatherState::Sunny).nor_else(in_state(SoilState::Fertilized)),
    ),
);

Equivalent logic can be achieved by using not in concert with or:

app.add_systems(
    slow_plant_growth.run_if(
        not(in_state(WeatherState::Sunny).or_else(in_state(SoilState::Fertilized))),
    ),
);
Source

fn nor_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> NorEager<Self::System, C::System>

Returns a new run condition that only returns true if both this one and the passed else_run return false.

The returned run condition is eagerly evaluated, meaning it will always execute both run conditions in order.

See also nor_else, which short-circuits if self returns true.

Source

fn nor<M, C: SystemCondition<M, In>>( self, else_run: C, ) -> NorElse<Self::System, C::System>

👎Deprecated since 0.19.0:

use .nor_else(...) instead, or .nor_eager(...) to evaluate the conditions eagerly

Returns a new run condition that only returns true if both this one and the passed else_run return false.

Source

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

Returns a new run condition that returns true if either this one or the passed or return true.

The returned run condition is short-circuiting, meaning or will only be invoked if self returns false.

Short-circuiting may not be desired in all cases; when utilizing change detection, the else_run condition will react to changes since the last time that self returned false, which may introduce subtle inconsistencies if short-circuiting was not intended. Similar issues may arise for run conditions that rely on internal state, such as those using Local<T> parameters or MessageReader<T>, as they may not be updated every time the combined condition is evaluated.

See also or_eager, which always evaluates both conditions.

§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(resource_exists::<B>)),
);
Source

fn or_eager<M, C: SystemCondition<M, In>>( self, other: C, ) -> OrEager<Self::System, C::System>

Returns a new run condition that returns true if either this one or the passed or return true.

The returned run condition is eagerly evaluated, meaning it will always execute both run conditions in order.

See also or_else, which short-circuits if self returns true.

Source

fn or<M, C: SystemCondition<M, In>>( self, else_run: C, ) -> OrElse<Self::System, C::System>

👎Deprecated since 0.19.0:

use .or_else(...) instead, or .or_eager(...) to eagerly evaluate both conditions

Returns a new run condition that returns true if either this one or the passed or return true.

Source

fn xnor<M, C: SystemCondition<M, In>>( self, other: C, ) -> Xnor<Self::System, C::System>

Returns a new run condition that only returns true if self and xnor both return false or both return true.

The returned run condition is eagerly evaluated, meaning it will always execute both run conditions in order.

§Examples
use bevy::prelude::*;

#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum CoffeeMachineState {
    Heating,
    Brewing,
    Inactive,
}

#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum TeaKettleState {
    Heating,
    Steeping,
    Inactive,
}

app.add_systems(
    // The take_drink_orders system will only execute if the `in_state(CoffeeMachineState::Inactive)`
    // run condition and `in_state(TeaKettleState::Inactive)` run conditions both evaluate to `false`,
    // or both evaluate to `true`.
    take_drink_orders.run_if(
        in_state(CoffeeMachineState::Inactive).xnor(in_state(TeaKettleState::Inactive))
    ),
);

Equivalent logic can be achieved by using not in concert with xor:

app.add_systems(
    take_drink_orders.run_if(
        not(in_state(CoffeeMachineState::Inactive).xor(in_state(TeaKettleState::Inactive)))
    ),
);
Source

fn xor<M, C: SystemCondition<M, In>>( self, other: C, ) -> Xor<Self::System, C::System>

Returns a new run condition that only returns true if either self or xor return true, but not both.

The returned run condition is eagerly evaluated, meaning it will always execute both run conditions in order.

§Examples
use bevy::prelude::*;

#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum CoffeeMachineState {
    Heating,
    Brewing,
    Inactive,
}

#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum TeaKettleState {
    Heating,
    Steeping,
    Inactive,
}

app.add_systems(
    // The prepare_beverage system will only execute if either the `in_state(CoffeeMachineState::Inactive)`
    // run condition or `in_state(TeaKettleState::Inactive)` run condition evaluates to `true`,
    // but not both.
    prepare_beverage.run_if(
        in_state(CoffeeMachineState::Inactive).xor(in_state(TeaKettleState::Inactive))
    ),
);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<Marker, In: SystemInput, F> SystemCondition<Marker, In> for F
where F: IntoSystem<In, bool, Marker, System: ReadOnlySystem>,