@@ -6,7 +6,7 @@ use crate::{
6
6
entity:: { Entities , Entity , EntityLocation } ,
7
7
event:: Event ,
8
8
observer:: { Observer , Observers } ,
9
- query:: Access ,
9
+ query:: { Access , ReadOnlyQueryData } ,
10
10
removal_detection:: RemovedComponentEvents ,
11
11
storage:: Storages ,
12
12
system:: IntoObserverSystem ,
@@ -156,6 +156,22 @@ impl<'w> EntityRef<'w> {
156
156
// SAFETY: We have read-only access to all components of this entity.
157
157
unsafe { self . 0 . get_by_id ( component_id) }
158
158
}
159
+
160
+ /// Returns read-only components for the current entity that match the query `Q`.
161
+ ///
162
+ /// # Panics
163
+ ///
164
+ /// If the entity does not have the components required by the query `Q`.
165
+ pub fn components < Q : ReadOnlyQueryData > ( & self ) -> Q :: Item < ' w > {
166
+ self . get_components :: < Q > ( ) . expect ( QUERY_MISMATCH_ERROR )
167
+ }
168
+
169
+ /// Returns read-only components for the current entity that match the query `Q`,
170
+ /// or `None` if the entity does not have the components required by the query `Q`.
171
+ pub fn get_components < Q : ReadOnlyQueryData > ( & self ) -> Option < Q :: Item < ' w > > {
172
+ // SAFETY: We have read-only access to all components of this entity.
173
+ unsafe { self . 0 . get_components :: < Q > ( ) }
174
+ }
159
175
}
160
176
161
177
impl < ' w > From < EntityWorldMut < ' w > > for EntityRef < ' w > {
@@ -351,6 +367,22 @@ impl<'w> EntityMut<'w> {
351
367
self . as_readonly ( ) . get ( )
352
368
}
353
369
370
+ /// Returns read-only components for the current entity that match the query `Q`.
371
+ ///
372
+ /// # Panics
373
+ ///
374
+ /// If the entity does not have the components required by the query `Q`.
375
+ pub fn components < Q : ReadOnlyQueryData > ( & self ) -> Q :: Item < ' _ > {
376
+ self . get_components :: < Q > ( ) . expect ( QUERY_MISMATCH_ERROR )
377
+ }
378
+
379
+ /// Returns read-only components for the current entity that match the query `Q`,
380
+ /// or `None` if the entity does not have the components required by the query `Q`.
381
+ pub fn get_components < Q : ReadOnlyQueryData > ( & self ) -> Option < Q :: Item < ' _ > > {
382
+ // SAFETY: We have read-only access to all components of this entity.
383
+ unsafe { self . 0 . get_components :: < Q > ( ) }
384
+ }
385
+
354
386
/// Consumes `self` and gets access to the component of type `T` with the
355
387
/// world `'w` lifetime for the current entity.
356
388
///
@@ -648,6 +680,23 @@ impl<'w> EntityWorldMut<'w> {
648
680
EntityRef :: from ( self ) . get ( )
649
681
}
650
682
683
+ /// Returns read-only components for the current entity that match the query `Q`.
684
+ ///
685
+ /// # Panics
686
+ ///
687
+ /// If the entity does not have the components required by the query `Q`.
688
+ #[ inline]
689
+ pub fn components < Q : ReadOnlyQueryData > ( & self ) -> Q :: Item < ' _ > {
690
+ EntityRef :: from ( self ) . components :: < Q > ( )
691
+ }
692
+
693
+ /// Returns read-only components for the current entity that match the query `Q`,
694
+ /// or `None` if the entity does not have the components required by the query `Q`.
695
+ #[ inline]
696
+ pub fn get_components < Q : ReadOnlyQueryData > ( & self ) -> Option < Q :: Item < ' _ > > {
697
+ EntityRef :: from ( self ) . get_components :: < Q > ( )
698
+ }
699
+
651
700
/// Consumes `self` and gets access to the component of type `T` with
652
701
/// the world `'w` lifetime for the current entity.
653
702
/// Returns `None` if the entity does not have a component of type `T`.
@@ -1491,6 +1540,8 @@ unsafe fn trigger_on_replace_and_on_remove_hooks_and_observers(
1491
1540
}
1492
1541
}
1493
1542
1543
+ const QUERY_MISMATCH_ERROR : & str = "Query does not match the current entity" ;
1544
+
1494
1545
/// A view into a single entity and component in a world, which may either be vacant or occupied.
1495
1546
///
1496
1547
/// This `enum` can only be constructed from the [`entry`] method on [`EntityWorldMut`].
@@ -1878,7 +1929,7 @@ impl<'w> FilteredEntityRef<'w> {
1878
1929
1879
1930
/// Returns an iterator over the component ids that are accessed by self.
1880
1931
#[ inline]
1881
- pub fn components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
1932
+ pub fn accessed_components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
1882
1933
self . access . component_reads_and_writes ( )
1883
1934
}
1884
1935
@@ -2135,7 +2186,7 @@ impl<'w> FilteredEntityMut<'w> {
2135
2186
2136
2187
/// Returns an iterator over the component ids that are accessed by self.
2137
2188
#[ inline]
2138
- pub fn components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
2189
+ pub fn accessed_components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
2139
2190
self . access . component_reads_and_writes ( )
2140
2191
}
2141
2192
@@ -3115,4 +3166,24 @@ mod tests {
3115
3166
assert ! ( e. get_mut_by_id( a_id) . is_none( ) ) ;
3116
3167
assert ! ( e. get_change_ticks_by_id( a_id) . is_none( ) ) ;
3117
3168
}
3169
+
3170
+ #[ test]
3171
+ fn get_components ( ) {
3172
+ #[ derive( Component , PartialEq , Eq , Debug ) ]
3173
+ struct X ( usize ) ;
3174
+
3175
+ #[ derive( Component , PartialEq , Eq , Debug ) ]
3176
+ struct Y ( usize ) ;
3177
+ let mut world = World :: default ( ) ;
3178
+ let e1 = world. spawn ( ( X ( 7 ) , Y ( 10 ) ) ) . id ( ) ;
3179
+ let e2 = world. spawn ( X ( 8 ) ) . id ( ) ;
3180
+ let e3 = world. spawn_empty ( ) . id ( ) ;
3181
+
3182
+ assert_eq ! (
3183
+ Some ( ( & X ( 7 ) , & Y ( 10 ) ) ) ,
3184
+ world. entity( e1) . get_components:: <( & X , & Y ) >( )
3185
+ ) ;
3186
+ assert_eq ! ( None , world. entity( e2) . get_components:: <( & X , & Y ) >( ) ) ;
3187
+ assert_eq ! ( None , world. entity( e3) . get_components:: <( & X , & Y ) >( ) ) ;
3188
+ }
3118
3189
}
0 commit comments