Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/bevy_state/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ pub trait CommandsStatesExt {
///
/// Note that commands introduce sync points to the ECS schedule, so modifying `NextState`
/// directly may be more efficient depending on your use-case.
fn set_state_if_neq<S: FreelyMutableState>(&mut self, state: S);
fn set_state_if_different<S: FreelyMutableState>(&mut self, state: S);
}

impl CommandsStatesExt for Commands<'_, '_> {
fn set_state<S: FreelyMutableState>(&mut self, state: S) {
self.queue(move |w: &mut World| {
let mut next = w.resource_mut::<NextState<S>>();
if let NextState::PendingIfNeq(prev) = &*next {
if let NextState::PendingIfDifferent(prev) = &*next {
debug!("overwriting next state {prev:?} with {state:?}");
}
next.set(state);
});
}

fn set_state_if_neq<S: FreelyMutableState>(&mut self, state: S) {
fn set_state_if_different<S: FreelyMutableState>(&mut self, state: S) {
self.queue(move |w: &mut World| {
let mut next = w.resource_mut::<NextState<S>>();
if let NextState::PendingIfNeq(prev) = &*next
if let NextState::PendingIfDifferent(prev) = &*next
&& *prev != state
{
debug!("overwriting next state {prev:?} with {state:?} if not equal");
debug!("overwriting next state {prev:?} with {state:?} if different");
}
next.set_if_neq(state);
next.set_if_different(state);
});
}
}
12 changes: 6 additions & 6 deletions crates/bevy_state/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub struct ReflectFreelyMutableState(ReflectFreelyMutableStateFns);
pub struct ReflectFreelyMutableStateFns {
/// Function pointer implementing [`ReflectFreelyMutableState::set_next_state()`].
pub set_next_state: fn(&mut World, &dyn Reflect, &TypeRegistry),
/// Function pointer implementing [`ReflectFreelyMutableState::set_next_state_if_neq()`].
pub set_next_state_if_neq: fn(&mut World, &dyn Reflect, &TypeRegistry),
/// Function pointer implementing [`ReflectFreelyMutableState::set_next_state_if_different()`].
pub set_next_state_if_different: fn(&mut World, &dyn Reflect, &TypeRegistry),
}

impl ReflectFreelyMutableStateFns {
Expand All @@ -80,13 +80,13 @@ impl ReflectFreelyMutableState {
(self.0.set_next_state)(world, state, registry);
}
/// Tentatively set a pending state transition to a reflected [`ReflectFreelyMutableState`], skipping state transitions if the target state is the same as the current state.
pub fn set_next_state_if_neq(
pub fn set_next_state_if_different(
&self,
world: &mut World,
state: &dyn Reflect,
registry: &TypeRegistry,
) {
(self.0.set_next_state_if_neq)(world, state, registry);
(self.0.set_next_state_if_different)(world, state, registry);
}
}

Expand All @@ -103,14 +103,14 @@ impl<S: FreelyMutableState + Reflect + TypePath> CreateTypeData<S> for ReflectFr
next_state.set(new_state);
}
},
set_next_state_if_neq: |world, reflected_state, registry| {
set_next_state_if_different: |world, reflected_state, registry| {
let new_state: S = from_reflect_with_fallback(
reflected_state.as_partial_reflect(),
world,
registry,
);
if let Some(mut next_state) = world.get_resource_mut::<NextState<S>>() {
next_state.set_if_neq(new_state);
next_state.set_if_different(new_state);
}
},
})
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_state/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ mod tests {
.is_empty());

world.insert_resource(TransitionCounter::default());
world.insert_resource(NextState::PendingIfNeq(SimpleState::A));
world.insert_resource(NextState::PendingIfDifferent(SimpleState::A));
world.run_schedule(StateTransition);
assert_eq!(world.resource::<State<SimpleState>>().0, SimpleState::A);
assert_eq!(
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_state/src/state/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ pub enum NextState<S: FreelyMutableState> {
/// There is a pending transition for state `S`
///
/// This will not trigger state transitions schedules if the target state is the same as the current one.
PendingIfNeq(S),
PendingIfDifferent(S),
}

impl<S: FreelyMutableState> NextState<S> {
/// Tentatively set a pending state transition to `Some(state)`.
///
/// This will run the state transition schedules [`OnEnter`](crate::state::OnEnter) and [`OnExit`](crate::state::OnExit).
/// If you want to skip those schedules for the same where we are transitioning to the same state, use [`set_if_neq`](Self::set_if_neq) instead.
/// If you want to skip those schedules when transitioning to the same state, use [`set_if_different`](Self::set_if_different) instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much simpler :)

pub fn set(&mut self, state: S) {
*self = Self::Pending(state);
}
Expand All @@ -203,9 +203,9 @@ impl<S: FreelyMutableState> NextState<S> {
///
/// Like [`set`](Self::set), but will not run any state transition schedules if the target state is the same as the current one.
/// If [`set`](Self::set) has already been called in the same frame with the same state, the transition schedules will be run anyways.
pub fn set_if_neq(&mut self, state: S) {
pub fn set_if_different(&mut self, state: S) {
if !matches!(self, Self::Pending(s) if s == &state) {
*self = Self::PendingIfNeq(state);
*self = Self::PendingIfDifferent(state);
}
}

Expand All @@ -225,7 +225,7 @@ pub(crate) fn take_next_state<S: FreelyMutableState>(
next_state.set_changed();
Some((x, true))
}
NextState::PendingIfNeq(x) => {
NextState::PendingIfDifferent(x) => {
next_state.set_changed();
Some((x, false))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_state/src/state_scoped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ mod tests {
let entity = app.world_mut().spawn(DespawnOnExit(State::On)).id();
assert!(app.world().get_entity(entity).is_ok());

app.world_mut().commands().set_state_if_neq(State::On);
app.world_mut().commands().set_state_if_different(State::On);
app.update();

assert_eq!(
Expand All @@ -878,7 +878,7 @@ mod tests {
&State::On
);
// entity was not despawned on exit
// this is because "set_state_if_neq" skips state transitions since
// this is because "set_state_if_different" skips state transitions since
// the app's next state is the same as its previous.
assert!(app.world().get_entity(entity).is_ok());
}
Expand Down