From b43c8f8c4f76b01263f8e76f3e2e4c962b68130d Mon Sep 17 00:00:00 2001 From: Brezak Date: Wed, 26 Feb 2025 00:07:44 +0100 Subject: [PATCH] Add methods to add single entity relationships (#18038) # Objective Closes #17572 ## Solution Add the `add_one_related` methods to `EntityCommands` and `EntityWorldMut`. ## Testing Clippy --- ## Showcase The `EntityWorldMut` and `FilteredResourcesMut` now include the `add_one_related` method if you just want to relate 2 entities. --- .../src/relationship/related_methods.rs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/relationship/related_methods.rs b/crates/bevy_ecs/src/relationship/related_methods.rs index 150bd02ebd..33e76b5874 100644 --- a/crates/bevy_ecs/src/relationship/related_methods.rs +++ b/crates/bevy_ecs/src/relationship/related_methods.rs @@ -21,7 +21,9 @@ impl<'w> EntityWorldMut<'w> { self } - /// Relates the given entities to this entity with the relation `R` + /// Relates the given entities to this entity with the relation `R`. + /// + /// See [`add_one_related`](Self::add_one_related) if you want relate only one entity. pub fn add_related(&mut self, related: &[Entity]) -> &mut Self { let id = self.id(); self.world_scope(|world| { @@ -32,6 +34,13 @@ impl<'w> EntityWorldMut<'w> { self } + /// Relates the given entity to this with the relation `R`. + /// + /// See [`add_related`](Self::add_related) if you want to relate more than one entity. + pub fn add_one_related(&mut self, entity: Entity) -> &mut Self { + self.add_related::(&[entity]) + } + /// Despawns entities that relate to this one via the given [`RelationshipTarget`]. /// This entity will not be despawned. pub fn despawn_related(&mut self) -> &mut Self { @@ -108,7 +117,9 @@ impl<'a> EntityCommands<'a> { self } - /// Relates the given entities to this entity with the relation `R` + /// Relates the given entities to this entity with the relation `R`. + /// + /// See [`add_one_related`](Self::add_one_related) if you want relate only one entity. pub fn add_related(&mut self, related: &[Entity]) -> &mut Self { let id = self.id(); let related = related.to_vec(); @@ -120,6 +131,13 @@ impl<'a> EntityCommands<'a> { self } + /// Relates the given entity to this with the relation `R`. + /// + /// See [`add_related`](Self::add_related) if you want to relate more than one entity. + pub fn add_one_related(&mut self, entity: Entity) -> &mut Self { + self.add_related::(&[entity]) + } + /// Despawns entities that relate to this one via the given [`RelationshipTarget`]. /// This entity will not be despawned. pub fn despawn_related(&mut self) -> &mut Self {