diff --git a/crates/bevy_ecs/src/hierarchy.rs b/crates/bevy_ecs/src/hierarchy.rs index 9f4b0d0f8f..ecdf854514 100644 --- a/crates/bevy_ecs/src/hierarchy.rs +++ b/crates/bevy_ecs/src/hierarchy.rs @@ -281,6 +281,12 @@ impl<'w> EntityWorldMut<'w> { self.add_related::(children) } + /// Removes all the children from this entity. + /// See also [`clear_related`](Self::clear_related) + pub fn clear_children(&mut self) -> &mut Self { + self.clear_related::() + } + /// Insert children at specific index. /// See also [`insert_related`](Self::insert_related). pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self { @@ -369,6 +375,12 @@ impl<'a> EntityCommands<'a> { self.add_related::(children) } + /// Removes all the children from this entity. + /// See also [`clear_related`](Self::clear_related) + pub fn clear_children(&mut self) -> &mut Self { + self.clear_related::() + } + /// Insert children at specific index. /// See also [`insert_related`](Self::insert_related). pub fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self { diff --git a/crates/bevy_ecs/src/relationship/related_methods.rs b/crates/bevy_ecs/src/relationship/related_methods.rs index 98ef8d0832..de4c01933e 100644 --- a/crates/bevy_ecs/src/relationship/related_methods.rs +++ b/crates/bevy_ecs/src/relationship/related_methods.rs @@ -47,6 +47,11 @@ impl<'w> EntityWorldMut<'w> { self } + /// Removes the relation `R` between this entity and all its related entities. + pub fn clear_related(&mut self) -> &mut Self { + self.remove::() + } + /// Relates the given entities to this entity with the relation `R`, starting at this particular index. /// /// 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> { }) } + /// Removes the relation `R` between this entity and all its related entities. + pub fn clear_related(&mut self) -> &mut Self { + self.queue(|mut entity: EntityWorldMut| { + entity.clear_related::(); + }) + } + /// Relates the given entities to this entity with the relation `R`, starting at this particular index. /// /// If the `related` has duplicates, a related entity will take the index of its last occurrence in `related`. @@ -613,4 +625,19 @@ mod tests { assert!(!world.entity(entity).contains::()); } } + + #[test] + fn remove_all_related() { + let mut world = World::new(); + + let a = world.spawn_empty().id(); + let b = world.spawn(ChildOf(a)).id(); + let c = world.spawn(ChildOf(a)).id(); + + world.entity_mut(a).clear_related::(); + + assert_eq!(world.entity(a).get::(), None); + assert_eq!(world.entity(b).get::(), None); + assert_eq!(world.entity(c).get::(), None); + } }