From 58c276ab440b7322b5e0245e4e9424dcb3386dd3 Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Thu, 12 Jun 2025 13:07:11 -0700 Subject: [PATCH] Make the `ObservedBy` component useful to public consumers (#19591) # Objective As raised by @Jondolf, this type is `pub`, and useful for various consumers to ensure cleanup or debugging. However, it doesn't offer any way to actually view the data. ## Solution - Add a read-only view of the data. - Don't add any (easy) way to mutate the data, as this presents a huge footgun. - Implement Reflect and register the component so you can see it in inspectors nicely. --- crates/bevy_app/src/app.rs | 3 +++ crates/bevy_ecs/src/observer/entity_observer.rs | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index a9d17c7a0b..8f1ef3c1b4 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -106,10 +106,13 @@ impl Default for App { #[cfg(feature = "bevy_reflect")] { + use bevy_ecs::observer::ObservedBy; + app.init_resource::(); app.register_type::(); app.register_type::(); app.register_type::(); + app.register_type::(); } #[cfg(feature = "reflect_functions")] diff --git a/crates/bevy_ecs/src/observer/entity_observer.rs b/crates/bevy_ecs/src/observer/entity_observer.rs index 74790a88b2..0e6d9d7781 100644 --- a/crates/bevy_ecs/src/observer/entity_observer.rs +++ b/crates/bevy_ecs/src/observer/entity_observer.rs @@ -6,12 +6,24 @@ use crate::{ }; use alloc::vec::Vec; +#[cfg(feature = "bevy_reflect")] +use crate::prelude::ReflectComponent; + use super::Observer; /// Tracks a list of entity observers for the [`Entity`] [`ObservedBy`] is added to. -#[derive(Default)] +#[derive(Default, Debug)] +#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))] +#[cfg_attr(feature = "bevy_reflect", reflect(Component, Debug))] pub struct ObservedBy(pub(crate) Vec); +impl ObservedBy { + /// Provides a read-only reference to the list of entities observing this entity. + pub fn get(&self) -> &[Entity] { + &self.0 + } +} + impl Component for ObservedBy { const STORAGE_TYPE: StorageType = StorageType::SparseSet; type Mutability = Mutable;