Query

Struct Query 

Source
pub struct Query<'a, T: QueryParameters> {
    pub data: <T as QueryParameterFetch<'a>>::FetchItem,
}
Expand description

Contains result of a QueryRunner execution providing access to entities and components in the World

Queries enable iteration over entities and their components. A query matches its parameters against the world to produce a series of results. Each query result is a tuple of components (the same components defined in the query) that belong to the same entity.

Computational cost of queries is reduced by the fact that they have an internal cache to avoid re-computing matches on each query run.

§Query as System Parameter

A Query can be used as a system parameter. Internally it create a QueryRunner and run it on each system execution.

§Immutable component access

The following example defines a query that gives an iterator over (&ComponentA, &ComponentB) tuples where ComponentA and ComponentB belong to the same entity

query: Query<(&ComponentA, &ComponentB)>

§Mutable component access

The following example is similar to the previous one, with the exception of ComponentA being accessed mutably here

mut query: Query<(&mut ComponentA, &ComponentB)>

§Add Entity ID to the Query

Inserting Entity at any position in the type parameter tuple will give access to the entity ID.

query: Query<(Entity, &ComponentA, &ComponentB)>

§Optional component access

A component can be made optional in a query by wrapping it into an Option. In the following example, the query will iterate over components of both entities that contain ComponentA and ComponentB, and entities that contain ComponentA but not ComponentB.

§Iteration over query result

The iter and iter_mut methods are used to iterate over query result. Refer to the Iterator API docs for advanced iterator usage.

use zengine_macro::Component;
use zengine_ecs::{
    World,
    query::{Query, QueryIter, QueryIterMut}
};

#[derive(Component, Debug)]
struct ComponentA {}

#[derive(Component, Debug)]
struct ComponentB {}

fn immutable_query(query: Query<(&ComponentA, &ComponentB)>) {
    for (a,b) in query.iter() {
        // a and b are immutable reference to ComponentA and ComponentB
    }
}

fn mutable_query(mut query: Query<(&mut ComponentA, &ComponentB)>) {
    for (mut a,b) in query.iter_mut() {
        // a is a mutable reference to ComponentA
        // b is an immutable reference to ComponentB
    }
}

Fields§

§data: <T as QueryParameterFetch<'a>>::FetchItem

Auto Trait Implementations§

§

impl<'a, T> Freeze for Query<'a, T>
where <T as QueryParameterFetch<'a>>::FetchItem: Freeze,

§

impl<'a, T> RefUnwindSafe for Query<'a, T>
where <T as QueryParameterFetch<'a>>::FetchItem: RefUnwindSafe,

§

impl<'a, T> Send for Query<'a, T>
where <T as QueryParameterFetch<'a>>::FetchItem: Send,

§

impl<'a, T> Sync for Query<'a, T>
where <T as QueryParameterFetch<'a>>::FetchItem: Sync,

§

impl<'a, T> Unpin for Query<'a, T>
where <T as QueryParameterFetch<'a>>::FetchItem: Unpin,

§

impl<'a, T> UnwindSafe for Query<'a, T>
where <T as QueryParameterFetch<'a>>::FetchItem: UnwindSafe,

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> 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, 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.