Run a system when a resource changes
Bevy version: 0.14Last updated:
Use resource_changed
in a run_if
condition.
use bevy::prelude::*;
#[derive(Resource)]
struct Counter(u32);
fn my_system(counter: Res<Counter>) {
println!("Counter: {}", counter.0);
}
fn main() {
App::new().add_systems(
Update,
// `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 addd a second
// condition to make sure the resource wasn't just added
.and_then(not(resource_added::<Counter>)),
)
);
}