Skip to content

Commit 3631a64

Browse files
authored
Add a method to clear all related entity to EntityCommands and friends (#18907)
# Objective We have methods to: - Add related entities - Replace related entities - Remove specific related entities We don't have a method the remove all related entities so. ## Solution Add a method to remove all related entities. ## Testing A new test case.
1 parent b40845d commit 3631a64

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

crates/bevy_ecs/src/hierarchy.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ impl<'w> EntityWorldMut<'w> {
281281
self.add_related::<ChildOf>(children)
282282
}
283283

284+
/// Removes all the children from this entity.
285+
/// See also [`clear_related`](Self::clear_related)
286+
pub fn clear_children(&mut self) -> &mut Self {
287+
self.clear_related::<ChildOf>()
288+
}
289+
284290
/// Insert children at specific index.
285291
/// See also [`insert_related`](Self::insert_related).
286292
pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {
@@ -369,6 +375,12 @@ impl<'a> EntityCommands<'a> {
369375
self.add_related::<ChildOf>(children)
370376
}
371377

378+
/// Removes all the children from this entity.
379+
/// See also [`clear_related`](Self::clear_related)
380+
pub fn clear_children(&mut self) -> &mut Self {
381+
self.clear_related::<ChildOf>()
382+
}
383+
372384
/// Insert children at specific index.
373385
/// See also [`insert_related`](Self::insert_related).
374386
pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self {

crates/bevy_ecs/src/relationship/related_methods.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ impl<'w> EntityWorldMut<'w> {
4747
self
4848
}
4949

50+
/// Removes the relation `R` between this entity and all its related entities.
51+
pub fn clear_related<R: Relationship>(&mut self) -> &mut Self {
52+
self.remove::<R::RelationshipTarget>()
53+
}
54+
5055
/// Relates the given entities to this entity with the relation `R`, starting at this particular index.
5156
///
5257
/// If the `related` has duplicates, a related entity will take the index of its last occurrence in `related`.
@@ -376,6 +381,13 @@ impl<'a> EntityCommands<'a> {
376381
})
377382
}
378383

384+
/// Removes the relation `R` between this entity and all its related entities.
385+
pub fn clear_related<R: Relationship>(&mut self) -> &mut Self {
386+
self.queue(|mut entity: EntityWorldMut| {
387+
entity.clear_related::<R>();
388+
})
389+
}
390+
379391
/// Relates the given entities to this entity with the relation `R`, starting at this particular index.
380392
///
381393
/// If the `related` has duplicates, a related entity will take the index of its last occurrence in `related`.
@@ -613,4 +625,19 @@ mod tests {
613625
assert!(!world.entity(entity).contains::<TestComponent>());
614626
}
615627
}
628+
629+
#[test]
630+
fn remove_all_related() {
631+
let mut world = World::new();
632+
633+
let a = world.spawn_empty().id();
634+
let b = world.spawn(ChildOf(a)).id();
635+
let c = world.spawn(ChildOf(a)).id();
636+
637+
world.entity_mut(a).clear_related::<ChildOf>();
638+
639+
assert_eq!(world.entity(a).get::<Children>(), None);
640+
assert_eq!(world.entity(b).get::<ChildOf>(), None);
641+
assert_eq!(world.entity(c).get::<ChildOf>(), None);
642+
}
616643
}

0 commit comments

Comments
 (0)