Skip to content

Commit 6628c6e

Browse files
Rename NextState::set_if_neq to set_if_different to resolve clashing (bevyengine#24676)
Fixes bevyengine#24655 by renaming NextState::set_if_neq to set_if_different to resolve clashing.
1 parent a8d1d0a commit 6628c6e

6 files changed

Lines changed: 67 additions & 18 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: "`NextState::set_if_neq` renamed to `set_if_different`"
3+
pull_requests: [24676]
4+
---
5+
6+
`NextState::set_if_neq` and related methods and enum variants have been renamed to avoid naming conflicts with `Mut::set_if_neq` / `ReflectMut::set_if_neq` which have a different meaning.
7+
8+
The following names have changed:
9+
10+
- `NextState::set_if_neq` is now `NextState::set_if_different`
11+
- `NextState::PendingIfNeq` is now `NextState::PendingIfDifferent`
12+
- `CommandsStatesExt::set_state_if_neq` is now `CommandsStatesExt::set_state_if_different`
13+
- `ReflectFreelyMutableState::set_next_state_if_neq` is now `ReflectFreelyMutableState::set_next_state_if_different`
14+
- `ReflectFreelyMutableStateFns::set_next_state_if_neq` is now `ReflectFreelyMutableStateFns::set_next_state_if_different`
15+
16+
Deprecated compatibility wrappers have been added for the renamed methods (`NextState::set_if_neq`, `CommandsStatesExt::set_state_if_neq`, and `ReflectFreelyMutableState::set_next_state_if_neq`) to allow existing code to compile with deprecation warnings.

crates/bevy_state/src/commands.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,43 @@ pub trait CommandsStatesExt {
2121
///
2222
/// Note that commands introduce sync points to the ECS schedule, so modifying `NextState`
2323
/// directly may be more efficient depending on your use-case.
24+
fn set_state_if_different<S: FreelyMutableState>(&mut self, state: S);
25+
26+
/// Sets the next state the app should move to, skipping any state transitions if the next state is the same as the current state.
27+
///
28+
/// Internally this schedules a command that updates the [`NextState<S>`](crate::prelude::NextState)
29+
/// resource with `state`.
30+
///
31+
/// Note that commands introduce sync points to the ECS schedule, so modifying `NextState`
32+
/// directly may be more efficient depending on your use-case.
33+
#[deprecated(since = "0.19.0", note = "use `set_state_if_different` instead")]
2434
fn set_state_if_neq<S: FreelyMutableState>(&mut self, state: S);
2535
}
2636

2737
impl CommandsStatesExt for Commands<'_, '_> {
2838
fn set_state<S: FreelyMutableState>(&mut self, state: S) {
2939
self.queue(move |w: &mut World| {
3040
let mut next = w.resource_mut::<NextState<S>>();
31-
if let NextState::PendingIfNeq(prev) = &*next {
41+
if let NextState::PendingIfDifferent(prev) = &*next {
3242
debug!("overwriting next state {prev:?} with {state:?}");
3343
}
3444
next.set(state);
3545
});
3646
}
3747

38-
fn set_state_if_neq<S: FreelyMutableState>(&mut self, state: S) {
48+
fn set_state_if_different<S: FreelyMutableState>(&mut self, state: S) {
3949
self.queue(move |w: &mut World| {
4050
let mut next = w.resource_mut::<NextState<S>>();
41-
if let NextState::PendingIfNeq(prev) = &*next
51+
if let NextState::PendingIfDifferent(prev) = &*next
4252
&& *prev != state
4353
{
44-
debug!("overwriting next state {prev:?} with {state:?} if not equal");
54+
debug!("overwriting next state {prev:?} with {state:?} if different");
4555
}
46-
next.set_if_neq(state);
56+
next.set_if_different(state);
4757
});
4858
}
59+
60+
fn set_state_if_neq<S: FreelyMutableState>(&mut self, state: S) {
61+
self.set_state_if_different(state);
62+
}
4963
}

crates/bevy_state/src/reflect.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pub struct ReflectFreelyMutableState(ReflectFreelyMutableStateFns);
5959
pub struct ReflectFreelyMutableStateFns {
6060
/// Function pointer implementing [`ReflectFreelyMutableState::set_next_state()`].
6161
pub set_next_state: fn(&mut World, &dyn Reflect, &TypeRegistry),
62-
/// Function pointer implementing [`ReflectFreelyMutableState::set_next_state_if_neq()`].
63-
pub set_next_state_if_neq: fn(&mut World, &dyn Reflect, &TypeRegistry),
62+
/// Function pointer implementing [`ReflectFreelyMutableState::set_next_state_if_different()`].
63+
pub set_next_state_if_different: fn(&mut World, &dyn Reflect, &TypeRegistry),
6464
}
6565

6666
impl ReflectFreelyMutableStateFns {
@@ -80,13 +80,23 @@ impl ReflectFreelyMutableState {
8080
(self.0.set_next_state)(world, state, registry);
8181
}
8282
/// Tentatively set a pending state transition to a reflected [`ReflectFreelyMutableState`], skipping state transitions if the target state is the same as the current state.
83+
pub fn set_next_state_if_different(
84+
&self,
85+
world: &mut World,
86+
state: &dyn Reflect,
87+
registry: &TypeRegistry,
88+
) {
89+
(self.0.set_next_state_if_different)(world, state, registry);
90+
}
91+
/// Tentatively set a pending state transition to a reflected [`ReflectFreelyMutableState`], skipping state transitions if the target state is the same as the current state.
92+
#[deprecated(since = "0.19.0", note = "use `set_next_state_if_different` instead")]
8393
pub fn set_next_state_if_neq(
8494
&self,
8595
world: &mut World,
8696
state: &dyn Reflect,
8797
registry: &TypeRegistry,
8898
) {
89-
(self.0.set_next_state_if_neq)(world, state, registry);
99+
self.set_next_state_if_different(world, state, registry);
90100
}
91101
}
92102

@@ -103,14 +113,14 @@ impl<S: FreelyMutableState + Reflect + TypePath> CreateTypeData<S> for ReflectFr
103113
next_state.set(new_state);
104114
}
105115
},
106-
set_next_state_if_neq: |world, reflected_state, registry| {
116+
set_next_state_if_different: |world, reflected_state, registry| {
107117
let new_state: S = from_reflect_with_fallback(
108118
reflected_state.as_partial_reflect(),
109119
world,
110120
registry,
111121
);
112122
if let Some(mut next_state) = world.get_resource_mut::<NextState<S>>() {
113-
next_state.set_if_neq(new_state);
123+
next_state.set_if_different(new_state);
114124
}
115125
},
116126
})

crates/bevy_state/src/state/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ mod tests {
581581
.is_empty());
582582

583583
world.insert_resource(TransitionCounter::default());
584-
world.insert_resource(NextState::PendingIfNeq(SimpleState::A));
584+
world.insert_resource(NextState::PendingIfDifferent(SimpleState::A));
585585
world.run_schedule(StateTransition);
586586
assert_eq!(world.resource::<State<SimpleState>>().0, SimpleState::A);
587587
assert_eq!(

crates/bevy_state/src/state/resources.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ pub enum NextState<S: FreelyMutableState> {
187187
/// There is a pending transition for state `S`
188188
///
189189
/// This will not trigger state transitions schedules if the target state is the same as the current one.
190-
PendingIfNeq(S),
190+
PendingIfDifferent(S),
191191
}
192192

193193
impl<S: FreelyMutableState> NextState<S> {
194194
/// Tentatively set a pending state transition to `Some(state)`.
195195
///
196196
/// This will run the state transition schedules [`OnEnter`](crate::state::OnEnter) and [`OnExit`](crate::state::OnExit).
197-
/// 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.
197+
/// If you want to skip those schedules when transitioning to the same state, use [`set_if_different`](Self::set_if_different) instead.
198198
pub fn set(&mut self, state: S) {
199199
*self = Self::Pending(state);
200200
}
@@ -203,12 +203,21 @@ impl<S: FreelyMutableState> NextState<S> {
203203
///
204204
/// Like [`set`](Self::set), but will not run any state transition schedules if the target state is the same as the current one.
205205
/// If [`set`](Self::set) has already been called in the same frame with the same state, the transition schedules will be run anyways.
206-
pub fn set_if_neq(&mut self, state: S) {
206+
pub fn set_if_different(&mut self, state: S) {
207207
if !matches!(self, Self::Pending(s) if s == &state) {
208-
*self = Self::PendingIfNeq(state);
208+
*self = Self::PendingIfDifferent(state);
209209
}
210210
}
211211

212+
/// Tentatively set a pending state transition to `Some(state)`.
213+
///
214+
/// Like [`set`](Self::set), but will not run any state transition schedules if the target state is the same as the current one.
215+
/// If [`set`](Self::set) has already been called in the same frame with the same state, the transition schedules will be run anyways.
216+
#[deprecated(since = "0.19.0", note = "use `set_if_different` instead")]
217+
pub fn set_if_neq(&mut self, state: S) {
218+
self.set_if_different(state);
219+
}
220+
212221
/// Remove any pending changes to [`State<S>`]
213222
pub fn reset(&mut self) {
214223
*self = Self::Unchanged;
@@ -225,7 +234,7 @@ pub(crate) fn take_next_state<S: FreelyMutableState>(
225234
next_state.set_changed();
226235
Some((x, true))
227236
}
228-
NextState::PendingIfNeq(x) => {
237+
NextState::PendingIfDifferent(x) => {
229238
next_state.set_changed();
230239
Some((x, false))
231240
}

crates/bevy_state/src/state_scoped.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ mod tests {
868868
let entity = app.world_mut().spawn(DespawnOnExit(State::On)).id();
869869
assert!(app.world().get_entity(entity).is_ok());
870870

871-
app.world_mut().commands().set_state_if_neq(State::On);
871+
app.world_mut().commands().set_state_if_different(State::On);
872872
app.update();
873873

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

0 commit comments

Comments
 (0)