diff --git a/crates/bevy_ecs/src/hierarchy.rs b/crates/bevy_ecs/src/hierarchy.rs index dfc32e60db..31c7b5e65a 100644 --- a/crates/bevy_ecs/src/hierarchy.rs +++ b/crates/bevy_ecs/src/hierarchy.rs @@ -294,6 +294,12 @@ impl<'w> EntityWorldMut<'w> { self.insert_related::(index, children) } + /// Insert child at specific index. + /// See also [`insert_related`](Self::insert_related). + pub fn insert_child(&mut self, index: usize, child: Entity) -> &mut Self { + self.insert_related::(index, &[child]) + } + /// Adds the given child to this entity /// See also [`add_related`](Self::add_related). pub fn add_child(&mut self, child: Entity) -> &mut Self { @@ -305,6 +311,11 @@ impl<'w> EntityWorldMut<'w> { self.remove_related::(children) } + /// Removes the relationship between this entity and the given entity. + pub fn remove_child(&mut self, child: Entity) -> &mut Self { + self.remove_related::(&[child]) + } + /// Replaces all the related children with a new set of children. pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self { self.replace_related::(children) @@ -374,6 +385,12 @@ impl<'a> EntityCommands<'a> { self.insert_related::(index, children) } + /// Insert children at specific index. + /// See also [`insert_related`](Self::insert_related). + pub fn insert_child(&mut self, index: usize, child: Entity) -> &mut Self { + self.insert_related::(index, &[child]) + } + /// Adds the given child to this entity pub fn add_child(&mut self, child: Entity) -> &mut Self { self.add_related::(&[child]) @@ -384,6 +401,11 @@ impl<'a> EntityCommands<'a> { self.remove_related::(children) } + /// Removes the relationship between this entity and the given entity. + pub fn remove_child(&mut self, child: Entity) -> &mut Self { + self.remove_related::(&[child]) + } + /// Replaces the children on this entity with a new list of children. pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self { self.replace_related::(children) @@ -641,6 +663,29 @@ mod tests { ); } + #[test] + fn insert_child() { + let mut world = World::new(); + let child1 = world.spawn_empty().id(); + let child2 = world.spawn_empty().id(); + let child3 = world.spawn_empty().id(); + + let mut entity_world_mut = world.spawn_empty(); + + let first_children = entity_world_mut.add_children(&[child1, child2]); + + let root = first_children.insert_child(1, child3).id(); + + let hierarchy = get_hierarchy(&world, root); + assert_eq!( + hierarchy, + Node::new_with( + root, + vec![Node::new(child1), Node::new(child3), Node::new(child2)] + ) + ); + } + // regression test for https://github.com/bevyengine/bevy/pull/19134 #[test] fn insert_children_index_bound() { @@ -698,6 +743,25 @@ mod tests { ); } + #[test] + fn remove_child() { + let mut world = World::new(); + let child1 = world.spawn_empty().id(); + let child2 = world.spawn_empty().id(); + let child3 = world.spawn_empty().id(); + + let mut root = world.spawn_empty(); + root.add_children(&[child1, child2, child3]); + root.remove_child(child2); + let root = root.id(); + + let hierarchy = get_hierarchy(&world, root); + assert_eq!( + hierarchy, + Node::new_with(root, vec![Node::new(child1), Node::new(child3)]) + ); + } + #[test] fn self_parenting_invalid() { let mut world = World::new();