From 7c55e5460bfb102adec8e06369d4f5c88b908e23 Mon Sep 17 00:00:00 2001 From: Anthony Kalaitzis Date: Thu, 22 Jun 2023 18:39:01 +0930 Subject: [PATCH 1/4] Change despawn descendants to return mutable self --- crates/bevy_hierarchy/src/hierarchy.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/bevy_hierarchy/src/hierarchy.rs b/crates/bevy_hierarchy/src/hierarchy.rs index d16ebd1904188..f0b6ed19ca3f8 100644 --- a/crates/bevy_hierarchy/src/hierarchy.rs +++ b/crates/bevy_hierarchy/src/hierarchy.rs @@ -86,7 +86,7 @@ pub trait DespawnRecursiveExt { fn despawn_recursive(self); /// Despawns all descendants of the given entity. - fn despawn_descendants(&mut self); + fn despawn_descendants(&mut self) -> &mut Self; } impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { @@ -96,9 +96,10 @@ impl<'w, 's, 'a> DespawnRecursiveExt for EntityCommands<'w, 's, 'a> { self.commands().add(DespawnRecursive { entity }); } - fn despawn_descendants(&mut self) { + fn despawn_descendants(&mut self) -> &mut Self { let entity = self.id(); self.commands().add(DespawnChildrenRecursive { entity }); + self } } @@ -117,7 +118,7 @@ impl<'w> DespawnRecursiveExt for EntityMut<'w> { despawn_with_children_recursive(self.into_world_mut(), entity); } - fn despawn_descendants(&mut self) { + fn despawn_descendants(&mut self) -> &mut Self { let entity = self.id(); #[cfg(feature = "trace")] @@ -130,6 +131,7 @@ impl<'w> DespawnRecursiveExt for EntityMut<'w> { self.world_scope(|world| { despawn_children_recursive(world, entity); }); + self } } From a206890888db685014ded1990ee54b4ed7f145fd Mon Sep 17 00:00:00 2001 From: Anthony Kalaitzis Date: Thu, 22 Jun 2023 18:41:01 +0930 Subject: [PATCH 2/4] Add test to remove and add children --- crates/bevy_hierarchy/src/hierarchy.rs | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/crates/bevy_hierarchy/src/hierarchy.rs b/crates/bevy_hierarchy/src/hierarchy.rs index f0b6ed19ca3f8..c94491af71d2e 100644 --- a/crates/bevy_hierarchy/src/hierarchy.rs +++ b/crates/bevy_hierarchy/src/hierarchy.rs @@ -248,4 +248,32 @@ mod tests { // The child should be despawned. assert!(world.get_entity(child).is_none()); } + + #[test] + fn spawn_children_after_despawn_descendants() { + let mut world = World::default(); + let mut queue = CommandQueue::default(); + let mut commands = Commands::new(&mut queue, &world); + + let parent = commands.spawn_empty().id(); + let child = commands.spawn_empty().id(); + + commands + .entity(parent) + .add_child(child) + .despawn_descendants() + .with_children(|parent| { + parent.spawn_empty(); + parent.spawn_empty(); + }); + + queue.apply(&mut world); + + // The parent's Children component should still have a two children. + let children = world.entity(parent).get::(); + assert!(children.is_some()); + assert!(children.unwrap().len() == 2 as usize); + // The original child should be despawned. + assert!(world.get_entity(child).is_none()); + } } From fde23c950336f3aec806842a9fc5988d13fee97f Mon Sep 17 00:00:00 2001 From: Anthony Kalaitzis Date: Thu, 22 Jun 2023 19:07:05 +0930 Subject: [PATCH 3/4] Amend usize int --- crates/bevy_hierarchy/src/hierarchy.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_hierarchy/src/hierarchy.rs b/crates/bevy_hierarchy/src/hierarchy.rs index c94491af71d2e..d4abc6f8c26d0 100644 --- a/crates/bevy_hierarchy/src/hierarchy.rs +++ b/crates/bevy_hierarchy/src/hierarchy.rs @@ -272,7 +272,7 @@ mod tests { // The parent's Children component should still have a two children. let children = world.entity(parent).get::(); assert!(children.is_some()); - assert!(children.unwrap().len() == 2 as usize); + assert!(children.unwrap().len() == 2_usize); // The original child should be despawned. assert!(world.get_entity(child).is_none()); } From c383e6c9a8f22e1a60ad9b6de3bd79c2b938c37b Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Thu, 22 Jun 2023 21:52:00 -0400 Subject: [PATCH 4/4] Fix typo --- crates/bevy_hierarchy/src/hierarchy.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_hierarchy/src/hierarchy.rs b/crates/bevy_hierarchy/src/hierarchy.rs index d4abc6f8c26d0..32c2f788d63aa 100644 --- a/crates/bevy_hierarchy/src/hierarchy.rs +++ b/crates/bevy_hierarchy/src/hierarchy.rs @@ -269,7 +269,7 @@ mod tests { queue.apply(&mut world); - // The parent's Children component should still have a two children. + // The parent's Children component should still have two children. let children = world.entity(parent).get::(); assert!(children.is_some()); assert!(children.unwrap().len() == 2_usize);