diff --git a/crates/bevy_ecs/src/relationship/related_methods.rs b/crates/bevy_ecs/src/relationship/related_methods.rs index de4c01933ef47..8c95859d992a5 100644 --- a/crates/bevy_ecs/src/relationship/related_methods.rs +++ b/crates/bevy_ecs/src/relationship/related_methods.rs @@ -1,10 +1,12 @@ use crate::{ bundle::Bundle, entity::{hash_set::EntityHashSet, Entity}, + event::Event, + observer::Observer, relationship::{ Relationship, RelationshipHookMode, RelationshipSourceCollection, RelationshipTarget, }, - system::{Commands, EntityCommands}, + system::{Commands, EntityCommands, IntoObserverSystem}, world::{EntityWorldMut, World}, }; use bevy_platform::prelude::{Box, Vec}; @@ -527,6 +529,17 @@ impl<'w, R: Relationship> RelatedSpawner<'w, R> { self.world.spawn(R::from(self.target)) } + /// Creates an [`Observer`] listening for events of type 'E' targeting the 'target' + /// entity this spawner was initialized with. No relationship 'R' is created + /// between the [`Observer`] and the 'target' entity. + pub fn observe_target( + &mut self, + observer: impl IntoObserverSystem, + ) -> EntityWorldMut<'_> { + self.world + .spawn(Observer::new(observer).with_entity(self.target)) + } + /// Returns the "target entity" used when spawning entities with an `R` [`Relationship`]. pub fn target_entity(&self) -> Entity { self.target