Skip to content

Commit b43c8f8

Browse files
authored
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.
1 parent 5961df7 commit b43c8f8

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

crates/bevy_ecs/src/relationship/related_methods.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ impl<'w> EntityWorldMut<'w> {
2121
self
2222
}
2323

24-
/// Relates the given entities to this entity with the relation `R`
24+
/// Relates the given entities to this entity with the relation `R`.
25+
///
26+
/// See [`add_one_related`](Self::add_one_related) if you want relate only one entity.
2527
pub fn add_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
2628
let id = self.id();
2729
self.world_scope(|world| {
@@ -32,6 +34,13 @@ impl<'w> EntityWorldMut<'w> {
3234
self
3335
}
3436

37+
/// Relates the given entity to this with the relation `R`.
38+
///
39+
/// See [`add_related`](Self::add_related) if you want to relate more than one entity.
40+
pub fn add_one_related<R: Relationship>(&mut self, entity: Entity) -> &mut Self {
41+
self.add_related::<R>(&[entity])
42+
}
43+
3544
/// Despawns entities that relate to this one via the given [`RelationshipTarget`].
3645
/// This entity will not be despawned.
3746
pub fn despawn_related<S: RelationshipTarget>(&mut self) -> &mut Self {
@@ -108,7 +117,9 @@ impl<'a> EntityCommands<'a> {
108117
self
109118
}
110119

111-
/// Relates the given entities to this entity with the relation `R`
120+
/// Relates the given entities to this entity with the relation `R`.
121+
///
122+
/// See [`add_one_related`](Self::add_one_related) if you want relate only one entity.
112123
pub fn add_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
113124
let id = self.id();
114125
let related = related.to_vec();
@@ -120,6 +131,13 @@ impl<'a> EntityCommands<'a> {
120131
self
121132
}
122133

134+
/// Relates the given entity to this with the relation `R`.
135+
///
136+
/// See [`add_related`](Self::add_related) if you want to relate more than one entity.
137+
pub fn add_one_related<R: Relationship>(&mut self, entity: Entity) -> &mut Self {
138+
self.add_related::<R>(&[entity])
139+
}
140+
123141
/// Despawns entities that relate to this one via the given [`RelationshipTarget`].
124142
/// This entity will not be despawned.
125143
pub fn despawn_related<S: RelationshipTarget>(&mut self) -> &mut Self {

0 commit comments

Comments
 (0)