pub struct SystemContext<'scope> { /* private fields */ }
Expand description
Thin wrapper over hecs::World
, can prepare queries using a
QueryMarker
.
Instantiating one directly is only useful when calling systems as plain functions,
and can be done either by SystemContext::new()
or converting a &hecs::World
or &mut hecs::World
:
fn some_system(_context: SystemContext, _resources: (), _queries: ()) {}
let mut world = hecs::World::new();
some_system(SystemContext::new(&world), (), ());
some_system((&world).into(), (), ());
some_system((&mut world).into(), (), ());
Implementations§
Source§impl<'scope> SystemContext<'scope>
impl<'scope> SystemContext<'scope>
Sourcepub fn new(world: &'scope World) -> Self
pub fn new(world: &'scope World) -> Self
Wraps a &hecs::World
. See documentation for SystemContext
itself.
Sourcepub fn id(&self) -> Option<SystemId>
pub fn id(&self) -> Option<SystemId>
Returns a debug-printable SystemId
if the system is ran in an
Executor
, with printed number reflecting
the order of insertion into the ExecutorBuilder
.
Sourcepub fn query<Q>(&self, _: QueryMarker<Q>) -> QueryBorrow<'_, Q>
pub fn query<Q>(&self, _: QueryMarker<Q>) -> QueryBorrow<'_, Q>
Prepares a query using the given QueryMarker
;
see hecs::World::query()
.
§Example
fn some_system(
context: SystemContext,
_resources: (),
query: QueryMarker<(&mut Pos, &Vel)>
) {
for (_entity, (pos, vel)) in context.query(query).iter() {
*pos += *vel;
}
};
Sourcepub fn query_one<Q>(
&self,
_: QueryMarker<Q>,
entity: Entity,
) -> Result<QueryOne<'_, Q>, NoSuchEntity>
pub fn query_one<Q>( &self, _: QueryMarker<Q>, entity: Entity, ) -> Result<QueryOne<'_, Q>, NoSuchEntity>
Prepares a query against a single entity using the given
QueryMarker
;
see hecs::World::query_one()
.
§Example
fn some_system(
context: SystemContext,
_resources: (),
query: QueryMarker<(&mut Pos, &Vel)>
) {
let mut max_velocity = Vel::default();
let mut max_velocity_entity = None;
for (entity, (pos, vel)) in context.query(query).iter() {
*pos += *vel;
if *vel > max_velocity {
max_velocity = *vel;
max_velocity_entity = Some(entity);
}
}
if let Some(entity) = max_velocity_entity {
let mut query_one = context
.query_one(query, entity)
.expect("no such entity");
let (pos, _vel) = query_one
.get()
.expect("some components are missing");
*pos = Pos::default();
}
};
Trait Implementations§
Source§impl<'scope> From<&'scope World> for SystemContext<'scope>
impl<'scope> From<&'scope World> for SystemContext<'scope>
Auto Trait Implementations§
impl<'scope> Freeze for SystemContext<'scope>
impl<'scope> !RefUnwindSafe for SystemContext<'scope>
impl<'scope> Send for SystemContext<'scope>
impl<'scope> Sync for SystemContext<'scope>
impl<'scope> Unpin for SystemContext<'scope>
impl<'scope> !UnwindSafe for SystemContext<'scope>
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
Mutably borrows from an owned value. Read more