Skip to content

Commit

Permalink
Fix RemoveChildren command (bevyengine#6192)
Browse files Browse the repository at this point in the history
# 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]>
  • Loading branch information
2 people authored and Pietrek14 committed Dec 17, 2022
1 parent 9be0c90 commit 1994262
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions crates/bevy_hierarchy/src/child_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
prelude::{Children, Parent},
HierarchyEvent,
};
use crate::{Children, HierarchyEvent, Parent};
use bevy_ecs::{
bundle::Bundle,
entity::Entity,
Expand Down Expand Up @@ -68,12 +65,19 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {

fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
let mut events: SmallVec<[HierarchyEvent; 8]> = SmallVec::new();
for child in children {
world.entity_mut(*child).remove::<Parent>();
events.push(HierarchyEvent::ChildRemoved {
child: *child,
parent,
});
if let Some(parent_children) = world.get::<Children>(parent) {
for &child in children {
if parent_children.contains(&child) {
events.push(HierarchyEvent::ChildRemoved { child, parent });
}
}
} else {
return;
}
for event in &events {
if let &HierarchyEvent::ChildRemoved { child, .. } = event {
world.entity_mut(child).remove::<Parent>();
}
}
push_events(world, events);

Expand Down

0 comments on commit 1994262

Please sign in to comment.