pub trait DetectChangesMut: DetectChanges {
    type Inner: ?Sized;

    // Required methods
    fn set_changed(&mut self);
    fn set_last_changed(&mut self, last_change_tick: u32);
    fn bypass_change_detection(&mut self) -> &mut Self::Inner;

    // Provided method
    fn set_if_neq(&mut self, value: Self::Inner)
       where Self::Inner: Sized + PartialEq<Self::Inner> { ... }
}
Expand description

Types that implement reliable change detection.

Example

Using types that implement DetectChangesMut, such as ResMut, provide a way to query if a value has been mutated in another system. Normally change detection is triggered by either DerefMut or AsMut, however it can be manually triggered via set_if_neq.

To ensure that changes are only triggered when the value actually differs, check if the value would change before assignment, such as by checking that new != old. You must be sure that you are not mutably dereferencing in this process.

set_if_neq is a helper method for this common functionality.

use bevy_ecs::prelude::*;

#[derive(Resource)]
struct MyResource(u32);

fn my_system(mut resource: ResMut<MyResource>) {
    if resource.is_changed() {
        println!("My resource was mutated!");
    }

   resource.0 = 42; // triggers change detection via [`DerefMut`]
}

Required Associated Types§

type Inner: ?Sized

The type contained within this smart pointer

For example, for ResMut<T> this would be T.

Required Methods§

fn set_changed(&mut self)

Flags this value as having been changed.

Mutably accessing this smart pointer will automatically flag this value as having been changed. However, mutation through interior mutability requires manual reporting.

Note: This operation cannot be undone.

fn set_last_changed(&mut self, last_change_tick: u32)

Manually sets the change tick recording the time when this data was last mutated.

Warning

This is a complex and error-prone operation, primarily intended for use with rollback networking strategies. If you merely want to flag this data as changed, use set_changed instead. If you want to avoid triggering change detection, use bypass_change_detection instead.

fn bypass_change_detection(&mut self) -> &mut Self::Inner

Manually bypasses change detection, allowing you to mutate the underlying value without updating the change tick.

Warning

This is a risky operation, that can have unexpected consequences on any system relying on this code. However, it can be an essential escape hatch when, for example, you are trying to synchronize representations using change detection and need to avoid infinite recursion.

Provided Methods§

fn set_if_neq(&mut self, value: Self::Inner)where Self::Inner: Sized + PartialEq<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).

This is useful to ensure change detection is only triggered when the underlying value changes, instead of every time DerefMut is used.

Implementors§

§

impl<'a> DetectChangesMut for MutUntyped<'a>

§

type Inner = PtrMut<'a, Aligned>

§

impl<'a, T> DetectChangesMut for NonSendMut<'a, T>where T: ?Sized,

§

type Inner = T

§

impl<'a, T> DetectChangesMut for ResMut<'a, T>where T: Resource + ?Sized,

§

type Inner = T

§

impl<'a, T> DetectChangesMut for Mut<'a, T>where T: ?Sized,

§

type Inner = T