From 315f3cab3684323f0c3cbaca53fd2ab3f8006e10 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Tue, 18 Apr 2023 17:18:09 +0300 Subject: [PATCH] Add `any_component_removed` condition (#8326) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- crates/bevy_ecs/src/schedule/condition.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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