pub struct Query<'a, Q: Query> { /* private fields */ }Expand description
Borrow of an ECS query result.
Obtained as a system parameter; derefs to hecs::QueryBorrow.
§Iterating
Query implements IntoIterator for &mut Query, so you can iterate it
directly without going through Deref:
fn move_system(mut q: Query<(&mut Position, &Velocity)>) {
for (entity, (pos, vel)) in &mut q {
pos.x += vel.x;
pos.y += vel.y;
}
}§Single-entity lookups
Use Query::get to fetch components for one known Entity without
scanning the whole result set, and Query::single /
Query::get_single when you expect exactly one match (e.g. “the
player”, “the active camera”).
Implementations§
Source§impl<'a, Q: Query> Query<'a, Q>
impl<'a, Q: Query> Query<'a, Q>
Sourcepub fn get(&self, entity: Entity) -> QueryOne<'_, Q>
pub fn get(&self, entity: Entity) -> QueryOne<'_, Q>
Look up a single entity’s components for this query. Returns
None if the entity doesn’t exist or doesn’t match Q.
Sourcepub fn with<R: Query>(self) -> QueryBorrow<'a, With<Q, R>>
pub fn with<R: Query>(self) -> QueryBorrow<'a, With<Q, R>>
Filter this query to only entities that ALSO have component R,
without R itself being part of the yielded items. Consumes
self — matches hecs’s own QueryBorrow::with signature.
Sourcepub fn without<R: Query>(self) -> QueryBorrow<'a, Without<Q, R>>
pub fn without<R: Query>(self) -> QueryBorrow<'a, Without<Q, R>>
Filter this query to only entities that do NOT have component R.
Consumes self, same reasoning as with.
Sourcepub fn single(&mut self) -> Q::Item<'_>
pub fn single(&mut self) -> Q::Item<'_>
Return the single entity’s components for this query.
Panics if there isn’t exactly one match. Intended for singleton-style
queries (the player, the active camera, …) where zero or multiple
matches indicate a bug. See Query::get_single for a
non-panicking version. Include hecs::Entity in Q if you need the
id alongside the components.
Sourcepub fn get_single(&mut self) -> Option<Q::Item<'_>>
pub fn get_single(&mut self) -> Option<Q::Item<'_>>
Like Query::single, but returns None instead of panicking when
there isn’t exactly one match.
Methods from Deref<Target = QueryBorrow<'a, Q>>§
Sourcepub fn iter_batched(&mut self, batch_size: u32) -> BatchedIter<'_, Q>
pub fn iter_batched(&mut self, batch_size: u32) -> BatchedIter<'_, Q>
Like iter, but returns child iterators of at most batch_size elements
Useful for distributing work over a threadpool.