Skip to main content

DetectChanges

Trait DetectChanges 

Source
pub trait DetectChanges {
    // Required methods
    fn is_added(&self) -> bool;
    fn is_changed(&self) -> bool;
    fn is_added_after(&self, other: Tick) -> bool;
    fn is_changed_after(&self, other: Tick) -> bool;
    fn last_changed(&self) -> Tick;
    fn added(&self) -> Tick;
    fn changed_by(&self) -> MaybeLocation;
}
Expand description

Types that can read change detection information. This change detection is controlled by DetectChangesMut types such as ResMut.

§Example

Using types that implement DetectChanges, such as Res, provide a way to query if a value has been mutated in another system.

use bevy_ecs::prelude::*;

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

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

Required Methods§

Source

fn is_added(&self) -> bool

Returns true if this value was added after the system last ran.

Source

fn is_changed(&self) -> bool

Returns true if this value was added or mutably dereferenced either since the last time the system ran or, if the system never ran, since the beginning of the program.

To check if the value was mutably dereferenced only, use this.is_changed() && !this.is_added().

Source

fn is_added_after(&self, other: Tick) -> bool

Returns true if this value was added after the other tick.

Source

fn is_changed_after(&self, other: Tick) -> bool

Returns true if this value was added or mutably dereferenced after the other tick.

§Example
fn system(query: Query<(Ref<Source>, &mut Target)>) {
    for (source, mut target) in query {
        // Only convert the source to the target if the source is newer
        if source.is_changed_after(target.last_changed()) {
            *target = Target::from_source(&source);
        }
    }
}
Source

fn last_changed(&self) -> Tick

Returns the change tick recording the time this data was most recently changed.

Note that components and resources are also marked as changed upon insertion.

For comparison, the previous change tick of a system can be read using the SystemChangeTick SystemParam.

Source

fn added(&self) -> Tick

Returns the change tick recording the time this data was added.

Source

fn changed_by(&self) -> MaybeLocation

The location that last caused this to change.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<'w, T: ?Sized + Resource<Mutability = Mutable>> DetectChanges for ResMut<'w, T>

Source§

impl<'w, T: ?Sized + Resource> DetectChanges for Res<'w, T>

Source§

impl<'w, T: ?Sized> DetectChanges for Mut<'w, T>

Source§

impl<'w, T: ?Sized> DetectChanges for NonSend<'w, T>

Source§

impl<'w, T: ?Sized> DetectChanges for NonSendMut<'w, T>

Source§

impl<'w, T: ?Sized> DetectChanges for Ref<'w, T>

Source§

impl<'w> DetectChanges for MutUntyped<'w>