Function bevy::ecs::schedule::common_conditions::state_changed
pub fn state_changed<S>() -> impl FnMut(Res<'_, State<S>>) + Clonewhere
S: States,
Expand description
Generates a Condition
-satisfying closure that returns true
if the state machine changed state.
To do things on transitions to/from specific states, use their respective OnEnter/OnExit schedules. Use this run condition if you want to detect any change, regardless of the value.
Panics
The condition will panic if the resource does not exist.
Example
#[derive(States, Clone, Copy, Default, Eq, PartialEq, Hash, Debug)]
enum GameState {
#[default]
Playing,
Paused,
}
world.init_resource::<State<GameState>>();
app.add_systems(
// `state_changed` will only return true if the
// given states value has just been updated or
// the state has just been added
my_system.run_if(state_changed::<GameState>()),
);
fn my_system(mut counter: ResMut<Counter>) {
counter.0 += 1;
}
// `GameState` has just been added so `my_system` will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);
// `GameState` has not been updated so `my_system` will not run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);
*world.resource_mut::<State<GameState>>() = State::new(GameState::Paused);
// Now that `GameState` has been updated `my_system` will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 2);