@@ -54,9 +54,9 @@ use log::warn;
54
54
/// # use bevy_ecs::prelude::*;
55
55
/// # let mut world = World::new();
56
56
/// let root = world.spawn_empty().id();
57
- /// let child1 = world.spawn(ChildOf { parent: root } ).id();
58
- /// let child2 = world.spawn(ChildOf { parent: root } ).id();
59
- /// let grandchild = world.spawn(ChildOf { parent: child1 } ).id();
57
+ /// let child1 = world.spawn(ChildOf( root) ).id();
58
+ /// let child2 = world.spawn(ChildOf( root) ).id();
59
+ /// let grandchild = world.spawn(ChildOf( child1) ).id();
60
60
///
61
61
/// assert_eq!(&**world.entity(root).get::<Children>().unwrap(), &[child1, child2]);
62
62
/// assert_eq!(&**world.entity(child1).get::<Children>().unwrap(), &[grandchild]);
@@ -96,9 +96,21 @@ use log::warn;
96
96
) ]
97
97
#[ relationship( relationship_target = Children ) ]
98
98
#[ doc( alias = "IsChild" , alias = "Parent" ) ]
99
- pub struct ChildOf {
99
+ pub struct ChildOf ( pub Entity ) ;
100
+
101
+ impl ChildOf {
102
+ /// The parent entity of this child entity.
103
+ #[ inline]
104
+ pub fn parent ( & self ) -> Entity {
105
+ self . 0
106
+ }
107
+
100
108
/// The parent entity of this child entity.
101
- pub parent : Entity ,
109
+ #[ deprecated( since = "0.16.0" , note = "Use child_of.parent() instead" ) ]
110
+ #[ inline]
111
+ pub fn get ( & self ) -> Entity {
112
+ self . 0
113
+ }
102
114
}
103
115
104
116
// TODO: We need to impl either FromWorld or Default so ChildOf can be registered as Reflect.
@@ -108,9 +120,7 @@ pub struct ChildOf {
108
120
impl FromWorld for ChildOf {
109
121
#[ inline( always) ]
110
122
fn from_world ( _world : & mut World ) -> Self {
111
- ChildOf {
112
- parent : Entity :: PLACEHOLDER ,
113
- }
123
+ ChildOf ( Entity :: PLACEHOLDER )
114
124
}
115
125
}
116
126
@@ -316,7 +326,7 @@ impl<'w> EntityWorldMut<'w> {
316
326
pub fn with_child ( & mut self , bundle : impl Bundle ) -> & mut Self {
317
327
let parent = self . id ( ) ;
318
328
self . world_scope ( |world| {
319
- world. spawn ( ( bundle, ChildOf { parent } ) ) ;
329
+ world. spawn ( ( bundle, ChildOf ( parent) ) ) ;
320
330
} ) ;
321
331
self
322
332
}
@@ -329,12 +339,9 @@ impl<'w> EntityWorldMut<'w> {
329
339
}
330
340
331
341
/// Inserts the [`ChildOf`] component with the given `parent` entity, if it exists.
332
- #[ deprecated(
333
- since = "0.16.0" ,
334
- note = "Use entity_mut.insert(ChildOf { parent: entity })"
335
- ) ]
342
+ #[ deprecated( since = "0.16.0" , note = "Use entity_mut.insert(ChildOf(entity))" ) ]
336
343
pub fn set_parent ( & mut self , parent : Entity ) -> & mut Self {
337
- self . insert ( ChildOf { parent } ) ;
344
+ self . insert ( ChildOf ( parent) ) ;
338
345
self
339
346
}
340
347
}
@@ -394,7 +401,7 @@ impl<'a> EntityCommands<'a> {
394
401
/// [`with_children`]: EntityCommands::with_children
395
402
pub fn with_child ( & mut self , bundle : impl Bundle ) -> & mut Self {
396
403
let parent = self . id ( ) ;
397
- self . commands . spawn ( ( bundle, ChildOf { parent } ) ) ;
404
+ self . commands . spawn ( ( bundle, ChildOf ( parent) ) ) ;
398
405
self
399
406
}
400
407
@@ -406,12 +413,9 @@ impl<'a> EntityCommands<'a> {
406
413
}
407
414
408
415
/// Inserts the [`ChildOf`] component with the given `parent` entity, if it exists.
409
- #[ deprecated(
410
- since = "0.16.0" ,
411
- note = "Use entity_commands.insert(ChildOf { parent: entity })"
412
- ) ]
416
+ #[ deprecated( since = "0.16.0" , note = "Use entity_commands.insert(ChildOf(entity))" ) ]
413
417
pub fn set_parent ( & mut self , parent : Entity ) -> & mut Self {
414
- self . insert ( ChildOf { parent } ) ;
418
+ self . insert ( ChildOf ( parent) ) ;
415
419
self
416
420
}
417
421
}
@@ -427,7 +431,7 @@ pub fn validate_parent_has_component<C: Component>(
427
431
return ;
428
432
} ;
429
433
if !world
430
- . get_entity ( child_of. parent )
434
+ . get_entity ( child_of. parent ( ) )
431
435
. is_ok_and ( |e| e. contains :: < C > ( ) )
432
436
{
433
437
// TODO: print name here once Name lives in bevy_ecs
@@ -527,9 +531,9 @@ mod tests {
527
531
fn hierarchy ( ) {
528
532
let mut world = World :: new ( ) ;
529
533
let root = world. spawn_empty ( ) . id ( ) ;
530
- let child1 = world. spawn ( ChildOf { parent : root } ) . id ( ) ;
531
- let grandchild = world. spawn ( ChildOf { parent : child1 } ) . id ( ) ;
532
- let child2 = world. spawn ( ChildOf { parent : root } ) . id ( ) ;
534
+ let child1 = world. spawn ( ChildOf ( root) ) . id ( ) ;
535
+ let grandchild = world. spawn ( ChildOf ( child1) ) . id ( ) ;
536
+ let child2 = world. spawn ( ChildOf ( root) ) . id ( ) ;
533
537
534
538
// Spawn
535
539
let hierarchy = get_hierarchy ( & world, root) ;
@@ -550,7 +554,7 @@ mod tests {
550
554
assert_eq ! ( hierarchy, Node :: new_with( root, vec![ Node :: new( child2) ] ) ) ;
551
555
552
556
// Insert
553
- world. entity_mut ( child1) . insert ( ChildOf { parent : root } ) ;
557
+ world. entity_mut ( child1) . insert ( ChildOf ( root) ) ;
554
558
let hierarchy = get_hierarchy ( & world, root) ;
555
559
assert_eq ! (
556
560
hierarchy,
@@ -638,7 +642,7 @@ mod tests {
638
642
fn self_parenting_invalid ( ) {
639
643
let mut world = World :: new ( ) ;
640
644
let id = world. spawn_empty ( ) . id ( ) ;
641
- world. entity_mut ( id) . insert ( ChildOf { parent : id } ) ;
645
+ world. entity_mut ( id) . insert ( ChildOf ( id ) ) ;
642
646
assert ! (
643
647
world. entity( id) . get:: <ChildOf >( ) . is_none( ) ,
644
648
"invalid ChildOf relationships should self-remove"
@@ -650,7 +654,7 @@ mod tests {
650
654
let mut world = World :: new ( ) ;
651
655
let parent = world. spawn_empty ( ) . id ( ) ;
652
656
world. entity_mut ( parent) . despawn ( ) ;
653
- let id = world. spawn ( ChildOf { parent } ) . id ( ) ;
657
+ let id = world. spawn ( ChildOf ( parent) ) . id ( ) ;
654
658
assert ! (
655
659
world. entity( id) . get:: <ChildOf >( ) . is_none( ) ,
656
660
"invalid ChildOf relationships should self-remove"
@@ -661,10 +665,10 @@ mod tests {
661
665
fn reinsert_same_parent ( ) {
662
666
let mut world = World :: new ( ) ;
663
667
let parent = world. spawn_empty ( ) . id ( ) ;
664
- let id = world. spawn ( ChildOf { parent } ) . id ( ) ;
665
- world. entity_mut ( id) . insert ( ChildOf { parent } ) ;
668
+ let id = world. spawn ( ChildOf ( parent) ) . id ( ) ;
669
+ world. entity_mut ( id) . insert ( ChildOf ( parent) ) ;
666
670
assert_eq ! (
667
- Some ( & ChildOf { parent } ) ,
671
+ Some ( & ChildOf ( parent) ) ,
668
672
world. entity( id) . get:: <ChildOf >( ) ,
669
673
"ChildOf should still be there"
670
674
) ;
@@ -699,11 +703,11 @@ mod tests {
699
703
700
704
assert_eq ! (
701
705
world. entity( child_a) . get:: <ChildOf >( ) . unwrap( ) ,
702
- & ChildOf { parent }
706
+ & ChildOf ( parent)
703
707
) ;
704
708
assert_eq ! (
705
709
world. entity( child_c) . get:: <ChildOf >( ) . unwrap( ) ,
706
- & ChildOf { parent }
710
+ & ChildOf ( parent)
707
711
) ;
708
712
assert ! ( world. entity( child_b) . get:: <ChildOf >( ) . is_none( ) ) ;
709
713
}
@@ -739,7 +743,7 @@ mod tests {
739
743
assert_eq ! ( children. 0 , [ child] ) ;
740
744
assert_eq ! (
741
745
world. entity( child) . get:: <ChildOf >( ) . unwrap( ) ,
742
- & ChildOf { parent }
746
+ & ChildOf ( parent)
743
747
) ;
744
748
}
745
749
@@ -762,11 +766,11 @@ mod tests {
762
766
763
767
assert_eq ! (
764
768
world. entity( child_a) . get:: <ChildOf >( ) . unwrap( ) ,
765
- & ChildOf { parent }
769
+ & ChildOf ( parent)
766
770
) ;
767
771
assert_eq ! (
768
772
world. entity( child_b) . get:: <ChildOf >( ) . unwrap( ) ,
769
- & ChildOf { parent }
773
+ & ChildOf ( parent)
770
774
) ;
771
775
assert_eq ! (
772
776
world. entity( parent) . get:: <Children >( ) . unwrap( ) . 0 ,
@@ -781,15 +785,15 @@ mod tests {
781
785
) ;
782
786
assert_eq ! (
783
787
world. entity( child_a) . get:: <ChildOf >( ) . unwrap( ) ,
784
- & ChildOf { parent }
788
+ & ChildOf ( parent)
785
789
) ;
786
790
assert_eq ! (
787
791
world. entity( child_c) . get:: <ChildOf >( ) . unwrap( ) ,
788
- & ChildOf { parent }
792
+ & ChildOf ( parent)
789
793
) ;
790
794
assert_eq ! (
791
795
world. entity( child_d) . get:: <ChildOf >( ) . unwrap( ) ,
792
- & ChildOf { parent }
796
+ & ChildOf ( parent)
793
797
) ;
794
798
assert_eq ! (
795
799
world. entity( parent) . get:: <Children >( ) . unwrap( ) . 0 ,
@@ -844,11 +848,11 @@ mod tests {
844
848
845
849
assert_eq ! (
846
850
world. entity( child_a) . get:: <ChildOf >( ) . unwrap( ) ,
847
- & ChildOf { parent }
851
+ & ChildOf ( parent)
848
852
) ;
849
853
assert_eq ! (
850
854
world. entity( child_b) . get:: <ChildOf >( ) . unwrap( ) ,
851
- & ChildOf { parent }
855
+ & ChildOf ( parent)
852
856
) ;
853
857
assert_eq ! (
854
858
world. entity( parent) . get:: <Children >( ) . unwrap( ) . 0 ,
@@ -863,11 +867,11 @@ mod tests {
863
867
) ;
864
868
assert_eq ! (
865
869
world. entity( child_c) . get:: <ChildOf >( ) . unwrap( ) ,
866
- & ChildOf { parent }
870
+ & ChildOf ( parent)
867
871
) ;
868
872
assert_eq ! (
869
873
world. entity( child_d) . get:: <ChildOf >( ) . unwrap( ) ,
870
- & ChildOf { parent }
874
+ & ChildOf ( parent)
871
875
) ;
872
876
assert_eq ! (
873
877
world. entity( parent) . get:: <Children >( ) . unwrap( ) . 0 ,
@@ -974,11 +978,10 @@ mod tests {
974
978
let mut world = World :: new ( ) ;
975
979
let parent = world. spawn_empty ( ) . id ( ) ;
976
980
let other = world. spawn_empty ( ) . id ( ) ;
977
- let child = world. spawn ( ChildOf { parent } ) . id ( ) ;
978
- world. entity_mut ( child) . insert_with_relationship_hook_mode (
979
- ChildOf { parent : other } ,
980
- RelationshipHookMode :: Skip ,
981
- ) ;
981
+ let child = world. spawn ( ChildOf ( parent) ) . id ( ) ;
982
+ world
983
+ . entity_mut ( child)
984
+ . insert_with_relationship_hook_mode ( ChildOf ( other) , RelationshipHookMode :: Skip ) ;
982
985
assert_eq ! (
983
986
& * * world. entity( parent) . get:: <Children >( ) . unwrap( ) ,
984
987
& [ child] ,
0 commit comments