Function bevy::prelude::state_changed

pub fn state_changed<S>(current_state: Option<Res<'_, State<S>>>) -> bool
where 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.


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


    // `state_changed` will only return true if the
    // given states value has just been updated or
    // the state has just been added

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

// `GameState` has just been added so `my_system` will run world);
assert_eq!(world.resource::<Counter>().0, 1);

// `GameState` has not been updated so `my_system` will not run 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 world);
assert_eq!(world.resource::<Counter>().0, 2);