Function bevy_ecs::schedule::common_conditions::in_state
source · pub fn in_state<S: States>(
state: S
) -> impl FnMut(Option<Res<'_, State<S>>>) -> bool + Clone
Expand description
Generates a Condition
-satisfying closure that returns true
if the state machine is currently in state
.
Will return false
if the state does not exist or if not in state
.
Example
#[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
enum GameState {
#[default]
Playing,
Paused,
}
world.init_resource::<State<GameState>>();
app.add_systems((
// `in_state` will only return true if the
// given state equals the given value
play_system.run_if(in_state(GameState::Playing)),
pause_system.run_if(in_state(GameState::Paused)),
));
fn play_system(mut counter: ResMut<Counter>) {
counter.0 += 1;
}
fn pause_system(mut counter: ResMut<Counter>) {
counter.0 -= 1;
}
// 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);