From 3996c79570fdd157bd459f185330e56d59cde4ff Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Fri, 7 Apr 2023 20:23:46 +0300 Subject: [PATCH 1/2] Add `any_component_removed` condition --- 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 041544c08e975..5a76a13a95bd2 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 entities with the given component 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 From eeeabf6c14f4bfee7c2b47881c6caf4ad9269c57 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Tue, 18 Apr 2023 16:11:58 +0300 Subject: [PATCH 2/2] Update crates/bevy_ecs/src/schedule/condition.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François --- crates/bevy_ecs/src/schedule/condition.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 5a76a13a95bd2..24d80040cf2af 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -895,7 +895,7 @@ pub mod common_conditions { } /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` - /// if there are any entities with the given component type removed. + /// 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.