diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 041544c08e..24d80040cf 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -143,6 +143,7 @@ pub mod common_conditions { change_detection::DetectChanges, event::{Event, EventReader}, prelude::{Component, Query, With}, + removal_detection::RemovedComponents, schedule::{State, States}, system::{IntoSystem, Res, Resource, System}, }; @@ -893,6 +894,17 @@ pub mod common_conditions { move |query: Query<(), With>| !query.is_empty() } + /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` + /// if there are any entity with a component of the given type removed. + pub fn any_component_removed() -> impl FnMut(RemovedComponents) -> bool { + // `RemovedComponents` based on events and therefore events need to be consumed, + // so that there are no false positives on subsequent calls of the run condition. + // Simply checking `is_empty` would not be enough. + // PERF: note that `count` is efficient (not actually looping/iterating), + // due to Bevy having a specialized implementation for events. + move |mut removals: RemovedComponents| !removals.iter().count() != 0 + } + /// Generates a [`Condition`](super::Condition) that inverses the result of passed one. /// /// # Example