diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 0ad7f0591744c..18668961c4ba1 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -685,14 +685,14 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// - /// *world.resource_mut::>() = State(GameState::Paused); + /// *world.resource_mut::>() = State::new(GameState::Paused); /// /// // Now that we are in `GameState::Pause`, `pause_system` will run /// app.run(&mut world); /// assert_eq!(world.resource::().0, 0); /// ``` pub fn in_state(state: S) -> impl FnMut(Res>) -> bool + Clone { - move |current_state: Res>| current_state.0 == state + move |current_state: Res>| *current_state == state } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` @@ -741,7 +741,7 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// - /// *world.resource_mut::>() = State(GameState::Paused); + /// *world.resource_mut::>() = State::new(GameState::Paused); /// /// // Now that we are in `GameState::Pause`, `pause_system` will run /// app.run(&mut world); @@ -751,7 +751,7 @@ pub mod common_conditions { state: S, ) -> impl FnMut(Option>>) -> bool + Clone { move |current_state: Option>>| match current_state { - Some(current_state) => current_state.0 == state, + Some(current_state) => *current_state == state, None => false, } } @@ -803,7 +803,7 @@ pub mod common_conditions { /// app.run(&mut world); /// assert_eq!(world.resource::().0, 1); /// - /// *world.resource_mut::>() = State(GameState::Paused); + /// *world.resource_mut::>() = State::new(GameState::Paused); /// /// // Now that `GameState` has been updated `my_system` will run /// app.run(&mut world); diff --git a/crates/bevy_ecs/src/schedule/state.rs b/crates/bevy_ecs/src/schedule/state.rs index 326c99d2afecd..1e856e0c46a02 100644 --- a/crates/bevy_ecs/src/schedule/state.rs +++ b/crates/bevy_ecs/src/schedule/state.rs @@ -83,7 +83,27 @@ pub struct OnUpdate(pub S); /// /// The starting state is defined via the [`Default`] implementation for `S`. #[derive(Resource, Default, Debug)] -pub struct State(pub S); +pub struct State(S); + +impl State { + /// Creates a new state with a specific value. + /// + /// To change the state use [`NextState`] rather than using this to modify the `State`. + pub fn new(state: S) -> Self { + Self(state) + } + + /// Get the current state. + pub fn get(&self) -> &S { + &self.0 + } +} + +impl PartialEq for State { + fn eq(&self, other: &S) -> bool { + self.get() == other + } +} /// The next state of [`State`]. ///