resource_changed

Function resource_changed 

Source
pub fn resource_changed<T>(res: Res<'_, T>) -> bool
where T: Resource,
Expand description

A SystemCondition-satisfying system that returns true if the resource of the given type has been added or mutably dereferenced since the condition was last checked.

Note that simply mutably dereferencing a resource is considered a change (DerefMut). Bevy does not compare resources to their previous values.

§Panics

The condition will panic if the resource does not exist.

§Example

app.add_systems(
    // `resource_changed` will only return true if the
    // given resource was just changed (or added)
    my_system.run_if(
        resource_changed::<Counter>
        // By default detecting changes will also trigger if the resource was
        // just added, this won't work with my example so I will add a second
        // condition to make sure the resource wasn't just added
        .and(not(resource_added::<Counter>))
    ),
);

fn my_system(mut counter: ResMut<Counter>) {
    counter.0 += 1;
}

// `Counter` hasn't been changed so `my_system` won't run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);

world.resource_mut::<Counter>().0 = 50;

// `Counter` was just changed so `my_system` will run
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 51);