Struct Single
pub struct Single<'w, 's, D, F = ()>where
D: IterQueryData,
F: QueryFilter,{ /* private fields */ }Expand description
System parameter that provides access to single entity’s components, much like Query::single/Query::single_mut.
This SystemParam fails validation if zero or more than one matching entity exists.
This will cause the system to be skipped, according to the rules laid out in SystemParamValidationError.
Use Option<Single<D, F>> instead if zero or one matching entities can exist.
Note that Single is not used as a search optimization. It is used as a validation with slight overhead compared to Query.
See Query for more details.
§Example
#[derive(Component)]
struct Hiding;
#[derive(Component)]
struct Boss {
health: f32
};
#[derive(Component)]
struct EnemySize {
height: f32
};
fn hurt_boss(mut boss: Single<&mut Boss, Without<Hiding>>) {
boss.health -= 4.0;
}
fn hurt_and_shrink_boss(mut boss_and_size: Single<(&mut Boss, &mut EnemySize)>) {
let (mut boss, mut size) = boss_and_size.into_inner();
boss.health -= 4.0;
size.height *= 0.5;
}Note that because Single implements Deref and DerefMut, methods and fields like health can be accessed directly.
You can also access the underlying data manually, by calling .deref/.deref_mut, or by using the * operator.
When mutable elements appear in Single, use .into_inner to extract the tuple elements to mutate them.
Implementations§
§impl<'w, 's, D, F> Single<'w, 's, D, F>where
D: IterQueryData,
F: QueryFilter,
impl<'w, 's, D, F> Single<'w, 's, D, F>where
D: IterQueryData,
F: QueryFilter,
pub fn into_inner(self) -> <D as QueryData>::Item<'w, 's>
pub fn into_inner(self) -> <D as QueryData>::Item<'w, 's>
Returns the inner item with ownership.
Trait Implementations§
§impl<'a, 'b, D, F> SystemParam for Single<'a, 'b, D, F>where
D: IterQueryData + 'static,
F: QueryFilter + 'static,
impl<'a, 'b, D, F> SystemParam for Single<'a, 'b, D, F>where
D: IterQueryData + 'static,
F: QueryFilter + 'static,
§type State = QueryState<D, F>
type State = QueryState<D, F>
§type Item<'w, 's> = Single<'w, 's, D, F>
type Item<'w, 's> = Single<'w, 's, D, F>
Self, instantiated with new lifetimes. Read more§fn init_state(world: &mut World) -> <Single<'a, 'b, D, F> as SystemParam>::State
fn init_state(world: &mut World) -> <Single<'a, 'b, D, F> as SystemParam>::State
State.§fn init_access(
state: &<Single<'a, 'b, D, F> as SystemParam>::State,
system_meta: &mut SystemMeta,
component_access_set: &mut FilteredAccessSet,
world: &mut World,
)
fn init_access( state: &<Single<'a, 'b, D, F> as SystemParam>::State, system_meta: &mut SystemMeta, component_access_set: &mut FilteredAccessSet, world: &mut World, )
§unsafe fn get_param<'w, 's>(
state: &'s mut <Single<'a, 'b, D, F> as SystemParam>::State,
system_meta: &SystemMeta,
world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> Result<<Single<'a, 'b, D, F> as SystemParam>::Item<'w, 's>, SystemParamValidationError>
unsafe fn get_param<'w, 's>( state: &'s mut <Single<'a, 'b, D, F> as SystemParam>::State, system_meta: &SystemMeta, world: UnsafeWorldCell<'w>, change_tick: Tick, ) -> Result<<Single<'a, 'b, D, F> as SystemParam>::Item<'w, 's>, SystemParamValidationError>
SystemParamFunction. Read more§fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
SystemParam]’s state.
This is used to apply Commands during ApplyDeferred.§fn queue(
state: &mut Self::State,
system_meta: &SystemMeta,
world: DeferredWorld<'_>,
)
fn queue( state: &mut Self::State, system_meta: &SystemMeta, world: DeferredWorld<'_>, )
ApplyDeferred.