pub struct Query { /* private fields */ }Expand description
Query iterates over entities matching QueryFilter
§Examples
use xuko_ecs::prelude::*;
let mut world = World::new();
let filter = QueryFilter::new()
.with_requires(&[component_id!(i32)]);
let queried = Query::query_world(&world, filter);
// do stuff with the queried entitiesImplementations§
Source§impl Query
impl Query
Sourcepub fn query_world(world: &World, filter: QueryFilter) -> Self
pub fn query_world(world: &World, filter: QueryFilter) -> Self
Queries entities that match filter on world
Examples found in repository?
examples/schedule.rs (lines 43-49)
42fn move_entities(world: &mut World) {
43 let entities = Query::query_world(
44 world,
45 QueryFilter {
46 requires: vec![component_id!(Position), component_id!(Velocity)],
47 ..Default::default()
48 },
49 );
50
51 for e in &entities {
52 let entity = world.get_entity_mut(e).unwrap();
53
54 let velocity = entity.get::<Velocity>().cloned().unwrap();
55 let position = entity.get_mut::<Position>().unwrap();
56
57 position.0 += velocity.0;
58 position.1 += velocity.1;
59
60 println!("Something that has a position: {position:?}");
61 }
62}More examples
examples/stress.rs (lines 20-27)
6fn main() {
7 const ENTIIES: u64 = 1_000_000;
8 let time = SystemTime::now();
9
10 let mut world = World::new();
11
12 #[allow(dead_code)]
13 struct Number(u64);
14
15 for n in 0..=ENTIIES {
16 let id = world.spawn();
17 world.get_entity_mut(id).unwrap().add(Number(n));
18 }
19
20 for e in &Query::query_world(
21 &world,
22 QueryFilter::new()
23 .with_requires(&[component_id!(Number)])
24 .with_func(Some(|id, world| {
25 world.get_entity(id).unwrap().get::<Number>().unwrap().0 % 147 == 0
26 })),
27 ) {
28 world.despawn(e);
29 }
30
31 println!(
32 "spawning and filtering {ENTIIES} entities took {}ms",
33 time.elapsed().unwrap().as_millis()
34 );
35}examples/basic.rs (lines 34-44)
4fn main() {
5 let mut world = World::new();
6
7 struct Name(String);
8 struct Age(u8);
9 struct Human;
10
11 let people = [
12 (Name("Alice".to_owned()), Age(20)),
13 (Name("Bob".to_owned()), Age(26)),
14 (Name("Cathrine".to_owned()), Age(33)),
15 (Name("Dennis".to_owned()), Age(10)),
16 ];
17
18 for (name, age) in people {
19 let entity = world.spawn();
20 let entity = world.get_entity_mut(entity).unwrap();
21
22 entity.add(name);
23 entity.add(age);
24 entity.add(Human);
25 }
26
27 {
28 let entity = world.spawn();
29 let entity = world.get_entity_mut(entity).unwrap();
30
31 entity.add(Name("Jupiter".to_owned()));
32 }
33
34 for e in &Query::query_world(
35 &world,
36 QueryFilter {
37 requires: vec![
38 component_id!(Name),
39 component_id!(Age),
40 component_id!(Human),
41 ],
42 ..Default::default()
43 },
44 ) {
45 let e = world.get_entity_mut(e).unwrap();
46 let age = e.get::<Age>().map(|x| Age(x.0)).unwrap();
47 let name = e.get_mut::<Name>().unwrap();
48
49 name.0 = name.0.to_uppercase();
50 println!("Found human named {}, who is {} years old", name.0, age.0);
51 }
52
53 for e in &Query::query_world(
54 &world,
55 QueryFilter {
56 requires: vec![component_id!(Name)],
57 ..Default::default()
58 },
59 ) {
60 let entity = world.get_entity(e).unwrap();
61 let name = entity.get::<Name>().unwrap();
62
63 println!("This entity (#{e}) has a name: {}", name.0);
64 }
65}Sourcepub fn filter_used(&self) -> Option<&QueryFilter>
pub fn filter_used(&self) -> Option<&QueryFilter>
Returns the QueryFilter used to create this Query
Trait Implementations§
Source§impl<'a> IntoIterator for &'a Query
impl<'a> IntoIterator for &'a Query
impl Eq for Query
Auto Trait Implementations§
impl Freeze for Query
impl RefUnwindSafe for Query
impl Send for Query
impl Sync for Query
impl Unpin for Query
impl UnwindSafe for Query
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more