bevy_ecs::schedule::common_conditions

Function resource_removed

source
pub fn resource_removed<T>(
    res: Option<Res<'_, T>>,
    existed: Local<'_, bool>,
) -> bool
where T: Resource,
Expand description

A Condition-satisfying system that returns true if the resource of the given type has been removed since the condition was last checked.

ยงExample

app.add_systems(
    // `resource_removed` will only return true if the
    // given resource was just removed
    my_system.run_if(resource_removed::<MyResource>),
);

#[derive(Resource, Default)]
struct MyResource;

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

world.init_resource::<MyResource>();

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

world.remove_resource::<MyResource>();

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