pub struct Query<'a, Q: Query> { /* private fields */ }Expand description
Borrow of an ECS query result — a curated wrapper around hecs’s query types, not a
transparent passthrough to them: no hecs::* type appears in any method here, and nothing
leaks through beyond what’s listed below.
§Iterating
iter returns a plain Iterator, so the usual adapters (.filter(...),
.map(...), .take(...), .collect(), …) all compose directly — reach for .filter(...)
for a predicate over the yielded values (health below a threshold, say); with/
without below are for filtering by which components an entity has, not
their values.
fn move_system(mut q: Query<(&mut Position, &Velocity)>) {
for (pos, vel) in q.iter() {
pos.x += vel.x;
pos.y += vel.y;
}
}Query also implements IntoIterator for &mut Query, so for item in &mut q works too,
identically to for item in q.iter().
Include hecs::Entity in Q (e.g. Query<(Entity, &Position)>) if you need the entity id
back alongside its components — it’s a query term like any other, not a separate mechanism.
§Narrowing by component (with/without)
with/without narrow which entities match, without adding
to (or needing) the yielded item type, and — unlike calling straight through to hecs —
chain: each returns another Query, so .with::<&Enemy>().without::<&Dead>().iter() keeps
every method on this page available at each step, no hecs::With/hecs::Without naming
required on your end.
§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 iter(&mut self) -> impl Iterator<Item = Q::Item<'_>>
pub fn iter(&mut self) -> impl Iterator<Item = Q::Item<'_>>
Iterate every entity matching this query. An ordinary Iterator — .filter(...),
.map(...), .count(), .collect(), and every other standard adapter work directly on
the result, no hecs types involved.
Sourcepub fn get(&mut self, entity: Entity) -> Option<Q::Item<'_>>
pub fn get(&mut self, entity: Entity) -> Option<Q::Item<'_>>
Look up a single entity’s components for this query. None if the entity doesn’t exist
or doesn’t match Q.
Sourcepub fn with<R: Query>(self) -> Query<'a, With<Q, R>>
pub fn with<R: Query>(self) -> Query<'a, With<Q, R>>
Narrow this query to only entities that ALSO have component(s) R, without R itself
being part of the yielded items. Consumes self and returns another Query — chain
further .with/.without, or call .iter/.get/.single directly on the result.
Sourcepub fn without<R: Query>(self) -> Query<'a, Without<Q, R>>
pub fn without<R: Query>(self) -> Query<'a, Without<Q, R>>
Narrow this query to only entities that do NOT have component(s) R. Same shape 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.
Trait Implementations§
Source§impl<'q, Q: Query> IntoIterator for &'q mut Query<'_, Q>
impl<'q, Q: Query> IntoIterator for &'q mut Query<'_, Q>
Source§impl<Q> SystemParam for Query<'static, Q>where
Q: Query + 'static,
impl<Q> SystemParam for Query<'static, Q>where
Q: Query + 'static,
type Item<'a> = Query<'a, Q>
type State = ()
fn fetch<'a>( _state: &'a mut Self::State, world: &'a World, _resources: &'a Resources, ) -> Self::Item<'a>
Source§fn requires() -> Vec<RequiredResource>
fn requires() -> Vec<RequiredResource>
App to validate — before
running a non-convergent stage’s systems — that every hard
requirement is already satisfied, failing fast with a clear message
instead of panicking deep inside whichever system happens to run
first. Read moreAuto Trait Implementations§
impl<'a, Q> Freeze for Query<'a, Q>
impl<'a, Q> RefUnwindSafe for Query<'a, Q>
impl<'a, Q> Send for Query<'a, Q>
impl<'a, Q> Sync for Query<'a, Q>
impl<'a, Q> Unpin for Query<'a, Q>where
Q: Unpin,
impl<'a, Q> UnsafeUnpin for Query<'a, Q>
impl<'a, Q> UnwindSafe for Query<'a, Q>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> Component for T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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> ⓘ
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> ⓘ
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