diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 273b9b119d..772caad356 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -119,12 +119,38 @@ pub trait DetectChangesMut: DetectChanges { /// you are trying to synchronize representations using change detection and need to avoid infinite recursion. fn bypass_change_detection(&mut self) -> &mut Self::Inner; - /// Sets `self` to `value`, if and only if `*self != *value` - /// - /// `T` is the type stored within the smart pointer (e.g. [`Mut`] or [`ResMut`]). + /// Overwrites this smart pointer with the given value, if and only if `*self != value` /// /// This is useful to ensure change detection is only triggered when the underlying value - /// changes, instead of every time [`DerefMut`] is used. + /// changes, instead of every time it is mutably accessed. + /// + /// # Examples + /// + /// ``` + /// # use bevy_ecs::{prelude::*, schedule::common_conditions::resource_changed}; + /// #[derive(Resource, PartialEq, Eq)] + /// pub struct Score(u32); + /// + /// fn reset_score(mut score: ResMut) { + /// // Set the score to zero, unless it is already zero. + /// score.set_if_neq(Score(0)); + /// } + /// # let mut world = World::new(); + /// # world.insert_resource(Score(1)); + /// # let mut score_changed = IntoSystem::into_system(resource_changed::()); + /// # score_changed.initialize(&mut world); + /// # score_changed.run((), &mut world); + /// # + /// # let mut schedule = Schedule::new(); + /// # schedule.add_systems(reset_score); + /// # + /// # // first time `reset_score` runs, the score is changed. + /// # schedule.run(&mut world); + /// # assert!(score_changed.run((), &mut world)); + /// # // second time `reset_score` runs, the score is not changed. + /// # schedule.run(&mut world); + /// # assert!(!score_changed.run((), &mut world)); + /// ``` #[inline] fn set_if_neq(&mut self, value: Self::Inner) where