@@ -294,6 +294,12 @@ impl<'w> EntityWorldMut<'w> {
294294 self . insert_related :: < ChildOf > ( index, children)
295295 }
296296
297+ /// Insert child at specific index.
298+ /// See also [`insert_related`](Self::insert_related).
299+ pub fn insert_child ( & mut self , index : usize , child : Entity ) -> & mut Self {
300+ self . insert_related :: < ChildOf > ( index, & [ child] )
301+ }
302+
297303 /// Adds the given child to this entity
298304 /// See also [`add_related`](Self::add_related).
299305 pub fn add_child ( & mut self , child : Entity ) -> & mut Self {
@@ -305,6 +311,11 @@ impl<'w> EntityWorldMut<'w> {
305311 self . remove_related :: < ChildOf > ( children)
306312 }
307313
314+ /// Removes the relationship between this entity and the given entity.
315+ pub fn remove_child ( & mut self , child : Entity ) -> & mut Self {
316+ self . remove_related :: < ChildOf > ( & [ child] )
317+ }
318+
308319 /// Replaces all the related children with a new set of children.
309320 pub fn replace_children ( & mut self , children : & [ Entity ] ) -> & mut Self {
310321 self . replace_related :: < ChildOf > ( children)
@@ -374,6 +385,12 @@ impl<'a> EntityCommands<'a> {
374385 self . insert_related :: < ChildOf > ( index, children)
375386 }
376387
388+ /// Insert children at specific index.
389+ /// See also [`insert_related`](Self::insert_related).
390+ pub fn insert_child ( & mut self , index : usize , child : Entity ) -> & mut Self {
391+ self . insert_related :: < ChildOf > ( index, & [ child] )
392+ }
393+
377394 /// Adds the given child to this entity
378395 pub fn add_child ( & mut self , child : Entity ) -> & mut Self {
379396 self . add_related :: < ChildOf > ( & [ child] )
@@ -384,6 +401,11 @@ impl<'a> EntityCommands<'a> {
384401 self . remove_related :: < ChildOf > ( children)
385402 }
386403
404+ /// Removes the relationship between this entity and the given entity.
405+ pub fn remove_child ( & mut self , child : Entity ) -> & mut Self {
406+ self . remove_related :: < ChildOf > ( & [ child] )
407+ }
408+
387409 /// Replaces the children on this entity with a new list of children.
388410 pub fn replace_children ( & mut self , children : & [ Entity ] ) -> & mut Self {
389411 self . replace_related :: < ChildOf > ( children)
@@ -641,6 +663,29 @@ mod tests {
641663 ) ;
642664 }
643665
666+ #[ test]
667+ fn insert_child ( ) {
668+ let mut world = World :: new ( ) ;
669+ let child1 = world. spawn_empty ( ) . id ( ) ;
670+ let child2 = world. spawn_empty ( ) . id ( ) ;
671+ let child3 = world. spawn_empty ( ) . id ( ) ;
672+
673+ let mut entity_world_mut = world. spawn_empty ( ) ;
674+
675+ let first_children = entity_world_mut. add_children ( & [ child1, child2] ) ;
676+
677+ let root = first_children. insert_child ( 1 , child3) . id ( ) ;
678+
679+ let hierarchy = get_hierarchy ( & world, root) ;
680+ assert_eq ! (
681+ hierarchy,
682+ Node :: new_with(
683+ root,
684+ vec![ Node :: new( child1) , Node :: new( child3) , Node :: new( child2) ]
685+ )
686+ ) ;
687+ }
688+
644689 // regression test for https://github.com/bevyengine/bevy/pull/19134
645690 #[ test]
646691 fn insert_children_index_bound ( ) {
@@ -698,6 +743,25 @@ mod tests {
698743 ) ;
699744 }
700745
746+ #[ test]
747+ fn remove_child ( ) {
748+ let mut world = World :: new ( ) ;
749+ let child1 = world. spawn_empty ( ) . id ( ) ;
750+ let child2 = world. spawn_empty ( ) . id ( ) ;
751+ let child3 = world. spawn_empty ( ) . id ( ) ;
752+
753+ let mut root = world. spawn_empty ( ) ;
754+ root. add_children ( & [ child1, child2, child3] ) ;
755+ root. remove_child ( child2) ;
756+ let root = root. id ( ) ;
757+
758+ let hierarchy = get_hierarchy ( & world, root) ;
759+ assert_eq ! (
760+ hierarchy,
761+ Node :: new_with( root, vec![ Node :: new( child1) , Node :: new( child3) ] )
762+ ) ;
763+ }
764+
701765 #[ test]
702766 fn self_parenting_invalid ( ) {
703767 let mut world = World :: new ( ) ;
0 commit comments