Function bevy::ecs::prelude::state_exists

source ·
pub fn state_exists<S>(current_state: Option<Res<'_, State<S>>>) -> bool
where S: States,
Expand description

A Condition-satisfying system that returns true if the state machine exists.

Example

#[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
enum GameState {
    #[default]
    Playing,
    Paused,
}

app.add_systems(
    // `state_exists` will only return true if the
    // given state exists
    my_system.run_if(state_exists::<GameState>),
);

fn my_system(mut counter: ResMut<Counter>) {
    counter.0 += 1;
}

// `GameState` does not yet exist `my_system` won't run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);

world.init_resource::<State<GameState>>();

// `GameState` now exists so `my_system` will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);