bevy_ecs::schedule::common_conditions

Function condition_changed

Source
pub fn condition_changed<Marker, CIn, C>(
    condition: C,
) -> impl Condition<(), CIn>
where CIn: SystemInput, C: Condition<Marker, CIn>,
Expand description

Generates a Condition that returns true when the passed one changes.

The first time this is called, the passed condition is assumed to have been previously false.

ยงExample

app.add_systems(
    my_system.run_if(condition_changed(resource_exists::<MyResource>)),
);

#[derive(Resource)]
struct MyResource;

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

// `MyResource` is initially there, the inner condition is true, the system runs once
world.insert_resource(MyResource);
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 1);

// We remove `MyResource`, the inner condition is now false, the system runs one more time.
world.remove_resource::<MyResource>();
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 2);
app.run(&mut world);
assert_eq!(world.resource::<Counter>().0, 2);