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 <justthecooldude@gmail.com>
This commit is contained in:
parent
b4accebe10
commit
1ca1c8c39e
@ -1,7 +1,4 @@
|
|||||||
use crate::{
|
use crate::{Children, HierarchyEvent, Parent};
|
||||||
prelude::{Children, Parent},
|
|
||||||
HierarchyEvent,
|
|
||||||
};
|
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
bundle::Bundle,
|
bundle::Bundle,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
@ -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) {
|
fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
|
||||||
let mut events: SmallVec<[HierarchyEvent; 8]> = SmallVec::new();
|
let mut events: SmallVec<[HierarchyEvent; 8]> = SmallVec::new();
|
||||||
for child in children {
|
if let Some(parent_children) = world.get::<Children>(parent) {
|
||||||
world.entity_mut(*child).remove::<Parent>();
|
for &child in children {
|
||||||
events.push(HierarchyEvent::ChildRemoved {
|
if parent_children.contains(&child) {
|
||||||
child: *child,
|
events.push(HierarchyEvent::ChildRemoved { child, parent });
|
||||||
parent,
|
}
|
||||||
});
|
}
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for event in &events {
|
||||||
|
if let &HierarchyEvent::ChildRemoved { child, .. } = event {
|
||||||
|
world.entity_mut(child).remove::<Parent>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
push_events(world, events);
|
push_events(world, events);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user