Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change despawn_descendants to return &mut Self #8928

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions crates/bevy_hierarchy/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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
}
}

Expand All @@ -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")]
Expand All @@ -130,6 +131,7 @@ impl<'w> DespawnRecursiveExt for EntityMut<'w> {
self.world_scope(|world| {
despawn_children_recursive(world, entity);
});
self
}
}

Expand Down Expand Up @@ -246,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.
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
let children = world.entity(parent).get::<Children>();
assert!(children.is_some());
assert!(children.unwrap().len() == 2_usize);
// The original child should be despawned.
assert!(world.get_entity(child).is_none());
}
}