Skip to content

Commit 8cd5316

Browse files
Add a despawn_children method to EntityWorldMut and EntityCommands (#19283)
# Objective At the moment, if someone wants to despawn all the children of an entity, they would need to use `despawn_related::<Children>();`. In my opinion, this makes a very common operation less easily discoverable and require some understanding of Entity Relationships. ## Solution Adding a `despawn_children ` makes a very simple, discoverable and readable way to despawn all the children while maintaining cohesion with other similar methods. ## Testing The implementation itself is very simple as it simply wraps around `despawn_related` with `Children` as the generic type. I gave it a quick try by modifying the parenting example and it worked as expected. --------- Co-authored-by: Zachary Harrold <[email protected]>
1 parent 20cd383 commit 8cd5316

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

crates/bevy_ecs/src/relationship/related_methods.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
bundle::Bundle,
33
entity::{hash_set::EntityHashSet, Entity},
4+
prelude::Children,
45
relationship::{
56
Relationship, RelationshipHookMode, RelationshipSourceCollection, RelationshipTarget,
67
},
@@ -302,6 +303,15 @@ impl<'w> EntityWorldMut<'w> {
302303
self
303304
}
304305

306+
/// Despawns the children of this entity.
307+
/// This entity will not be despawned.
308+
///
309+
/// This is a specialization of [`despawn_related`](EntityWorldMut::despawn_related), a more general method for despawning via relationships.
310+
pub fn despawn_children(&mut self) -> &mut Self {
311+
self.despawn_related::<Children>();
312+
self
313+
}
314+
305315
/// Inserts a component or bundle of components into the entity and all related entities,
306316
/// traversing the relationship tracked in `S` in a breadth-first manner.
307317
///
@@ -467,6 +477,14 @@ impl<'a> EntityCommands<'a> {
467477
})
468478
}
469479

480+
/// Despawns the children of this entity.
481+
/// This entity will not be despawned.
482+
///
483+
/// This is a specialization of [`despawn_related`](EntityCommands::despawn_related), a more general method for despawning via relationships.
484+
pub fn despawn_children(&mut self) -> &mut Self {
485+
self.despawn_related::<Children>()
486+
}
487+
470488
/// Inserts a component or bundle of components into the entity and all related entities,
471489
/// traversing the relationship tracked in `S` in a breadth-first manner.
472490
///

0 commit comments

Comments
 (0)