pub fn state_exists_and_equals<S: States>(
    state: S
) -> impl FnMut(Option<Res<'_, State<S>>>) -> bool + Clone
👎Deprecated since 0.13.0: use in_state instead.
Expand description

Identical to in_state - use that instead.

Generates a Condition-satisfying closure that returns true if the state machine exists and is currently in state.

The condition will return false if the state does not exist.

Example

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

app.add_systems((
    // `state_exists_and_equals` will only return true if the
    // given state exists and equals the given value
    play_system.run_if(state_exists_and_equals(GameState::Playing)),
    pause_system.run_if(state_exists_and_equals(GameState::Paused)),
));

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

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

// `GameState` does not yet exists so neither system will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);

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

// We default to `GameState::Playing` so `play_system` runs
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);

*world.resource_mut::<State<GameState>>() = State::new(GameState::Paused);

// Now that we are in `GameState::Pause`, `pause_system` will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);