Skip to content

Commit 315f3ca

Browse files
Shaturmockersf
andauthored
Add any_component_removed condition (#8326)
Added helper extracted from #7711. that PR contains some controversy conditions, but this one should be good to go. --- ## Changelog ### Added - `any_component_removed` condition. --------- Co-authored-by: François <[email protected]>
1 parent 09df19b commit 315f3ca

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

crates/bevy_ecs/src/schedule/condition.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub mod common_conditions {
143143
change_detection::DetectChanges,
144144
event::{Event, EventReader},
145145
prelude::{Component, Query, With},
146+
removal_detection::RemovedComponents,
146147
schedule::{State, States},
147148
system::{IntoSystem, Res, Resource, System},
148149
};
@@ -893,6 +894,17 @@ pub mod common_conditions {
893894
move |query: Query<(), With<T>>| !query.is_empty()
894895
}
895896

897+
/// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
898+
/// if there are any entity with a component of the given type removed.
899+
pub fn any_component_removed<T: Component>() -> impl FnMut(RemovedComponents<T>) -> bool {
900+
// `RemovedComponents` based on events and therefore events need to be consumed,
901+
// so that there are no false positives on subsequent calls of the run condition.
902+
// Simply checking `is_empty` would not be enough.
903+
// PERF: note that `count` is efficient (not actually looping/iterating),
904+
// due to Bevy having a specialized implementation for events.
905+
move |mut removals: RemovedComponents<T>| !removals.iter().count() != 0
906+
}
907+
896908
/// Generates a [`Condition`](super::Condition) that inverses the result of passed one.
897909
///
898910
/// # Example

0 commit comments

Comments
 (0)