Skip to content

Commit e15cc8b

Browse files
Rename clear_child* to detach_child* (#21470)
# Objective Fixes #21329 - The naming of the `clear_children` function could be misunderstood, so we suggest to rename it. The current `clear_children` function could be understood to despawn the children, which it does not do. To emphasize the difference between `clear` and `despawn`, we can rename `clear` to `detach`. So we arrive at the final naming of `despawn` and `detach`. ## Solution To improve this, we could rename `clear_children` to `detach_children` and all other `clear`/`remove` functions too. I also suggest to rename all `remove_child*` functions for the same reason. This renaming resulted in a confict because two methods now had the same name: - `detach_children(self)`, previously `clear_children` - `detach_children(self, children: &[Entity])`, previously `remove_children` To solve this, I propose we rename the former to `detach_all_children`. This was chosen because it has the least impact, considering that all methods do take a slice of children, except this one. Changing the `insert_*` and `add_*` functions should be discussed. I think there is less potential for confusion, because they require you to pass an existing entity. Therefore it should be clear that this does not spawn anything. Note: The function `ui_surface.try_remove_children` has not been renamed. ## Testing Currently running the tests, but there are unrelated compile errors in wgpu-hal blocking me (I am on windows). For this reason, this is a draft pull request. Feel free to provide feedback already, while I am trying to make the tests run. ## Discussion As proposed in this PR, this would be a breaking change and will likely affect a large number of downstream projects directly. If you think this should be handled differently, via a `#[deprecated]` flag, I can rework the code to do that. Is there no documentation to update? The repo yielded no more search results than this. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent 0d6cb16 commit e15cc8b

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

crates/bevy_ecs/src/hierarchy.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,23 @@ impl<'w> EntityWorldMut<'w> {
277277
self
278278
}
279279

280-
/// Adds the given children to this entity
280+
/// Adds the given children to this entity.
281281
/// See also [`add_related`](Self::add_related).
282282
pub fn add_children(&mut self, children: &[Entity]) -> &mut Self {
283283
self.add_related::<ChildOf>(children)
284284
}
285285

286286
/// Removes all the children from this entity.
287287
/// See also [`clear_related`](Self::clear_related)
288+
#[deprecated = "Use detach_all_children() instead"]
288289
pub fn clear_children(&mut self) -> &mut Self {
290+
self.detach_all_children()
291+
}
292+
293+
/// Removes all the parent-child relationships from this entity.
294+
/// To despawn the child entities, instead use [`EntityWorldMut::despawn_children`](EntityWorldMut::despawn_children).
295+
/// See also [`clear_related`](Self::clear_related)
296+
pub fn detach_all_children(&mut self) -> &mut Self {
289297
self.clear_related::<ChildOf>()
290298
}
291299

@@ -301,19 +309,33 @@ impl<'w> EntityWorldMut<'w> {
301309
self.insert_related::<ChildOf>(index, &[child])
302310
}
303311

304-
/// Adds the given child to this entity
312+
/// Adds the given child to this entity.
305313
/// See also [`add_related`](Self::add_related).
306314
pub fn add_child(&mut self, child: Entity) -> &mut Self {
307315
self.add_related::<ChildOf>(&[child])
308316
}
309317

310318
/// Removes the relationship between this entity and the given entities.
319+
#[deprecated = "Use detach_children() instead"]
311320
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
321+
self.detach_children(children)
322+
}
323+
324+
/// Removes the parent-child relationship between this entity and the given entities.
325+
/// Does not despawn the children.
326+
pub fn detach_children(&mut self, children: &[Entity]) -> &mut Self {
312327
self.remove_related::<ChildOf>(children)
313328
}
314329

315330
/// Removes the relationship between this entity and the given entity.
331+
#[deprecated = "Use detach_child() instead"]
316332
pub fn remove_child(&mut self, child: Entity) -> &mut Self {
333+
self.detach_child(child)
334+
}
335+
336+
/// Removes the parent-child relationship between this entity and the given entity.
337+
/// Does not despawn the child.
338+
pub fn detach_child(&mut self, child: Entity) -> &mut Self {
317339
self.remove_related::<ChildOf>(&[child])
318340
}
319341

@@ -369,14 +391,22 @@ impl<'a> EntityCommands<'a> {
369391
self
370392
}
371393

372-
/// Adds the given children to this entity
394+
/// Adds the given children to this entity.
373395
pub fn add_children(&mut self, children: &[Entity]) -> &mut Self {
374396
self.add_related::<ChildOf>(children)
375397
}
376398

377399
/// Removes all the children from this entity.
378400
/// See also [`clear_related`](Self::clear_related)
401+
#[deprecated = "Use detach_all_children() instead"]
379402
pub fn clear_children(&mut self) -> &mut Self {
403+
self.detach_all_children()
404+
}
405+
406+
/// Removes all the parent-child relationships from this entity.
407+
/// To despawn the child entities, instead use [`EntityWorldMut::despawn_children`](EntityWorldMut::despawn_children).
408+
/// See also [`clear_related`](Self::clear_related)
409+
pub fn detach_all_children(&mut self) -> &mut Self {
380410
self.clear_related::<ChildOf>()
381411
}
382412

@@ -392,18 +422,32 @@ impl<'a> EntityCommands<'a> {
392422
self.insert_related::<ChildOf>(index, &[child])
393423
}
394424

395-
/// Adds the given child to this entity
425+
/// Adds the given child to this entity.
396426
pub fn add_child(&mut self, child: Entity) -> &mut Self {
397427
self.add_related::<ChildOf>(&[child])
398428
}
399429

400430
/// Removes the relationship between this entity and the given entities.
431+
#[deprecated = "Use detach_children() instead"]
401432
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
433+
self.detach_children(children)
434+
}
435+
436+
/// Removes the parent-child relationship between this entity and the given entities.
437+
/// Does not despawn the children.
438+
pub fn detach_children(&mut self, children: &[Entity]) -> &mut Self {
402439
self.remove_related::<ChildOf>(children)
403440
}
404441

405442
/// Removes the relationship between this entity and the given entity.
443+
#[deprecated = "Use detach_child() instead"]
406444
pub fn remove_child(&mut self, child: Entity) -> &mut Self {
445+
self.detach_child(child)
446+
}
447+
448+
/// Removes the parent-child relationship between this entity and the given entity.
449+
/// Does not despawn the child.
450+
pub fn detach_child(&mut self, child: Entity) -> &mut Self {
407451
self.remove_related::<ChildOf>(&[child])
408452
}
409453

@@ -731,7 +775,7 @@ mod tests {
731775
}
732776

733777
#[test]
734-
fn remove_children() {
778+
fn detach_children() {
735779
let mut world = World::new();
736780
let child1 = world.spawn_empty().id();
737781
let child2 = world.spawn_empty().id();
@@ -740,7 +784,7 @@ mod tests {
740784

741785
let mut root = world.spawn_empty();
742786
root.add_children(&[child1, child2, child3, child4]);
743-
root.remove_children(&[child2, child3]);
787+
root.detach_children(&[child2, child3]);
744788
let root = root.id();
745789

746790
let hierarchy = get_hierarchy(&world, root);
@@ -751,15 +795,15 @@ mod tests {
751795
}
752796

753797
#[test]
754-
fn remove_child() {
798+
fn detach_child() {
755799
let mut world = World::new();
756800
let child1 = world.spawn_empty().id();
757801
let child2 = world.spawn_empty().id();
758802
let child3 = world.spawn_empty().id();
759803

760804
let mut root = world.spawn_empty();
761805
root.add_children(&[child1, child2, child3]);
762-
root.remove_child(child2);
806+
root.detach_child(child2);
763807
let root = root.id();
764808

765809
let hierarchy = get_hierarchy(&world, root);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Renamed `clear_children` methods to `detach_all_children`
3+
pull_requests: [21470]
4+
---
5+
6+
In summary, we renamed `clear_*` and `remove_*` methods to `detach_*`.
7+
This should clarify that these methods do not despawn the child entities.
8+
9+
We renamed several related methods on both `EntityCommands` and `EntityWorldMut`:
10+
11+
- The method `EntityCommands::clear_children` has been renamed to `EntityCommands::detach_all_children`.
12+
- The method `EntityWorldMut::clear_children` has been renamed to `EntityWorldMut::detach_all_children`.
13+
- The method `EntityCommands::remove_children` has been renamed to `EntityCommands::detach_children`.
14+
- The method `EntityWorldMut::remove_children` has been renamed to `EntityWorldMut::detach_children`.
15+
- The method `EntityCommands::remove_child` has been renamed to `EntityCommands::detach_child`.
16+
- The method `EntityWorldMut::remove_child` has been renamed to `EntityWorldMut::detach_child`.

0 commit comments

Comments
 (0)