@@ -119,12 +119,38 @@ pub trait DetectChangesMut: DetectChanges {
119
119
/// you are trying to synchronize representations using change detection and need to avoid infinite recursion.
120
120
fn bypass_change_detection ( & mut self ) -> & mut Self :: Inner ;
121
121
122
- /// Sets `self` to `value`, if and only if `*self != *value`
123
- ///
124
- /// `T` is the type stored within the smart pointer (e.g. [`Mut`] or [`ResMut`]).
122
+ /// Overwrites this smart pointer with the given value, if and only if `*self != value`
125
123
///
126
124
/// This is useful to ensure change detection is only triggered when the underlying value
127
- /// changes, instead of every time [`DerefMut`] is used.
125
+ /// changes, instead of every time it is mutably accessed.
126
+ ///
127
+ /// # Examples
128
+ ///
129
+ /// ```
130
+ /// # use bevy_ecs::{prelude::*, schedule::common_conditions::resource_changed};
131
+ /// #[derive(Resource, PartialEq, Eq)]
132
+ /// pub struct Score(u32);
133
+ ///
134
+ /// fn reset_score(mut score: ResMut<Score>) {
135
+ /// // Set the score to zero, unless it is already zero.
136
+ /// score.set_if_neq(Score(0));
137
+ /// }
138
+ /// # let mut world = World::new();
139
+ /// # world.insert_resource(Score(1));
140
+ /// # let mut score_changed = IntoSystem::into_system(resource_changed::<Score>());
141
+ /// # score_changed.initialize(&mut world);
142
+ /// # score_changed.run((), &mut world);
143
+ /// #
144
+ /// # let mut schedule = Schedule::new();
145
+ /// # schedule.add_systems(reset_score);
146
+ /// #
147
+ /// # // first time `reset_score` runs, the score is changed.
148
+ /// # schedule.run(&mut world);
149
+ /// # assert!(score_changed.run((), &mut world));
150
+ /// # // second time `reset_score` runs, the score is not changed.
151
+ /// # schedule.run(&mut world);
152
+ /// # assert!(!score_changed.run((), &mut world));
153
+ /// ```
128
154
#[ inline]
129
155
fn set_if_neq ( & mut self , value : Self :: Inner )
130
156
where
0 commit comments