Skip to content

Commit 1ca1c8c

Browse files
committed
Fix RemoveChildren command (#6192)
# Objective `RemoveChildren` could remove the `Parent` component from children belonging to a different parent, which breaks the hierarchy. This change looks a little funny because I'm reusing the events to avoid needing to clone the parent's `Children`. Co-authored-by: devil-ira <[email protected]>
1 parent b4acceb commit 1ca1c8c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

crates/bevy_hierarchy/src/child_builder.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
prelude::{Children, Parent},
3-
HierarchyEvent,
4-
};
1+
use crate::{Children, HierarchyEvent, Parent};
52
use bevy_ecs::{
63
bundle::Bundle,
74
entity::Entity,
@@ -68,12 +65,19 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
6865

6966
fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
7067
let mut events: SmallVec<[HierarchyEvent; 8]> = SmallVec::new();
71-
for child in children {
72-
world.entity_mut(*child).remove::<Parent>();
73-
events.push(HierarchyEvent::ChildRemoved {
74-
child: *child,
75-
parent,
76-
});
68+
if let Some(parent_children) = world.get::<Children>(parent) {
69+
for &child in children {
70+
if parent_children.contains(&child) {
71+
events.push(HierarchyEvent::ChildRemoved { child, parent });
72+
}
73+
}
74+
} else {
75+
return;
76+
}
77+
for event in &events {
78+
if let &HierarchyEvent::ChildRemoved { child, .. } = event {
79+
world.entity_mut(child).remove::<Parent>();
80+
}
7781
}
7882
push_events(world, events);
7983

0 commit comments

Comments
 (0)