Function bevy_internal::ecs::prelude::state_changed
source · pub fn state_changed<S>(current_state: Option<Res<'_, State<S>>>) -> boolwhere
S: States,
Expand description
A Condition
-satisfying system 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.
Returns false if the state does not exist or the state has not changed.
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);