Query

Struct Query 

Source
pub struct Query<T>(/* private fields */)
where
    T: IsQuery + ?Sized;
Expand description

Queries are used to iterate over matching bundles of components.

Querys are written as a tuple of component column types, where each position in the tuple denotes a column value in each item of the resulting query iterator. Each item column type will be wrapped in Entry in the resulting query iterator.

For example: Query<(&mut f32, &String)> will result in an iterator of items with the type (&mut Entry<f32>, &Entry<String>).

§Creating queries

Some functions query immidiately and return QueryGuard:

With these functions you pass component column types as a type parameter:

let mut query = world
    .get_components_mut()
    .query::<(&f32, &String)>();
for (f32, string) in query.iter_mut() {
    //...
}

§System data queries

Query implements Edges, which allows you to use queries as fields inside system data structs:

#[derive(Edges)]
struct MySystemData {
    counter: ViewMut<u32>,
    // we use &'static to avoid introducing a lifetime
    q_f32_and_string: Query<(&'static f32, &'static String)>,
}

Which means queries may be visited from outside the world, or used as the input to a system:

use apecs::*;

#[derive(Edges)]
struct MySystemData {
    tracker: ViewMut<u64>,
    // We can use Mut and Ref which are aliases for &'static mut and &'static.
    q_f32_and_string: Query<(Mut<f32>, Ref<String>)>,
}

fn my_system(mut data: MySystemData) -> Result<(), GraphError> {
    for (f32, string) in data.q_f32_and_string.query().iter_mut() {
        if f32.was_modified_since(*data.tracker) {
            **f32 += 1.0;
            println!("set entity {} = {}", f32.id(), **f32);
        }
    }
    *data.tracker = apecs::current_iteration();
    ok()
}

let mut world = World::default();
world.add_subgraph(graph!(my_system));
world.tick().unwrap();

§Iterating queries

Below we use mutable and immutable reference types in our query to include either a mutable or immutable reference to those components:

use apecs::*;

struct Position(pub f32);
struct Velocity(pub f32);
struct Acceleration(pub f32);

fn create(mut entities: ViewMut<Entities>) -> Result<(), GraphError> {
    for i in 0..100 {
        entities.create().insert_bundle((
            Position(0.0),
            Velocity(0.0),
            Acceleration(i as f32),
        ));
    }
     
    /// This system ends after one tick
    end()
}

fn accelerate(q_accelerate: Query<(&mut Velocity, &Acceleration)>) -> Result<(), GraphError> {
    for (v, a) in q_accelerate.query().iter_mut() {
        v.0 += a.0;
    }
    ok()
}

fn position(q_move: Query<(&mut Position, &Velocity)>) -> Result<(), GraphError> {
    for (p, v) in q_move.query().iter_mut() {
        p.0 += v.0;
    }
    ok()
}

let mut world = World::default();
world.add_subgraph(graph!(create, accelerate, position));
world.tick().unwrap();

Implementations§

Source§

impl<Q> Query<Q>
where Q: IsQuery + ?Sized,

Source

pub fn query(&self) -> QueryGuard<'_, Q>

Prepare the query, locking the columns needed to iterate over matching components.

Trait Implementations§

Source§

impl<T> Edges for Query<T>
where T: IsQuery + Send + Sync + ?Sized,

Source§

fn reads() -> Vec<TypeKey>

Keys of all read types used in fields in the implementor.
Source§

fn writes() -> Vec<TypeKey>

Keys of all write types used in fields in the implementor.
Source§

fn moves() -> Vec<TypeKey>

Keys of all move types used in fields in the implementor.
Source§

fn construct(loan_mngr: &mut TypeMap) -> Result<Self, GraphError>

Attempt to construct the implementor from the given TypeMap.

Auto Trait Implementations§

§

impl<T> Freeze for Query<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for Query<T>

§

impl<T> Send for Query<T>
where T: Send + ?Sized,

§

impl<T> Sync for Query<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for Query<T>
where T: Unpin + ?Sized,

§

impl<T> !UnwindSafe for Query<T>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
Source§

impl<T> SatisfyTraits<dyn None> for T

Source§

impl<T> SatisfyTraits<dyn Send> for T
where T: Send,

Source§

impl<T> SatisfyTraits<dyn Send + Sync> for T
where T: Send + Sync,

Source§

impl<T> SatisfyTraits<dyn Sync> for T
where T: Sync,