Skip to content

Commit 045ef4c

Browse files
authored
Add remove_children and remove_related to EntityWorldMut and EntityCommands (#18835)
Fixes #18834. `EntityWorldMut::remove_children` and `EntityCommands::remove_children` were removed in the relationships overhaul (#17398) and never got replaced. I don't *think* this was intentional (the methods were never mentioned in the PR or its comments), but I could've missed something.
1 parent 5e69518 commit 045ef4c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

crates/bevy_ecs/src/hierarchy.rs

+30
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ impl<'w> EntityWorldMut<'w> {
293293
self.add_related::<ChildOf>(&[child])
294294
}
295295

296+
/// Removes the relationship between this entity and the given entities.
297+
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
298+
self.remove_related::<ChildOf>(children)
299+
}
300+
296301
/// Replaces all the related children with a new set of children.
297302
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
298303
self.replace_related::<ChildOf>(children)
@@ -375,6 +380,11 @@ impl<'a> EntityCommands<'a> {
375380
self.add_related::<ChildOf>(&[child])
376381
}
377382

383+
/// Removes the relationship between this entity and the given entities.
384+
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
385+
self.remove_related::<ChildOf>(children)
386+
}
387+
378388
/// Replaces the children on this entity with a new list of children.
379389
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
380390
self.replace_related::<ChildOf>(children)
@@ -646,6 +656,26 @@ mod tests {
646656
);
647657
}
648658

659+
#[test]
660+
fn remove_children() {
661+
let mut world = World::new();
662+
let child1 = world.spawn_empty().id();
663+
let child2 = world.spawn_empty().id();
664+
let child3 = world.spawn_empty().id();
665+
let child4 = world.spawn_empty().id();
666+
667+
let mut root = world.spawn_empty();
668+
root.add_children(&[child1, child2, child3, child4]);
669+
root.remove_children(&[child2, child3]);
670+
let root = root.id();
671+
672+
let hierarchy = get_hierarchy(&world, root);
673+
assert_eq!(
674+
hierarchy,
675+
Node::new_with(root, vec![Node::new(child1), Node::new(child4)])
676+
);
677+
}
678+
649679
#[test]
650680
fn self_parenting_invalid() {
651681
let mut world = World::new();

crates/bevy_ecs/src/relationship/related_methods.rs

+26
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ impl<'w> EntityWorldMut<'w> {
105105
self
106106
}
107107

108+
/// Removes the relation `R` between this entity and the given entities.
109+
pub fn remove_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
110+
let id = self.id();
111+
self.world_scope(|world| {
112+
for related in related {
113+
if world
114+
.get::<R>(*related)
115+
.is_some_and(|relationship| relationship.get() == id)
116+
{
117+
world.entity_mut(*related).remove::<R>();
118+
}
119+
}
120+
});
121+
122+
self
123+
}
124+
108125
/// Replaces all the related entities with a new set of entities.
109126
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
110127
type Collection<R> =
@@ -383,6 +400,15 @@ impl<'a> EntityCommands<'a> {
383400
self.add_related::<R>(&[entity])
384401
}
385402

403+
/// Removes the relation `R` between this entity and the given entities.
404+
pub fn remove_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
405+
let related: Box<[Entity]> = related.into();
406+
407+
self.queue(move |mut entity: EntityWorldMut| {
408+
entity.remove_related::<R>(&related);
409+
})
410+
}
411+
386412
/// Replaces all the related entities with the given set of new related entities.
387413
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
388414
let related: Box<[Entity]> = related.into();

0 commit comments

Comments
 (0)