Query

Struct Query 

Source
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 entities

Implementations§

Source§

impl Query

Source

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
Hide additional 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}
Source

pub fn filter_used(&self) -> Option<&QueryFilter>

Returns the QueryFilter used to create this Query

Source

pub fn iter(&self) -> Iter<'_>

Return an iterator over the query

Trait Implementations§

Source§

impl Clone for Query

Source§

fn clone(&self) -> Query

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Query

Source§

fn default() -> Query

Returns the “default value” for a type. Read more
Source§

impl<'a> IntoIterator for &'a Query

Source§

type Item = usize

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Query

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Component for T
where T: Send + Sync + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,