pub struct Entity { /* private fields */ }Expand description
Lightweight identifier of an entity.
The identifier is implemented using a generational index: a combination of an index and a generation. This allows fast insertion after data removal in an array while minimizing loss of spatial locality.
§Usage
This data type is returned by iterating a Query that has Entity as part of its query fetch type parameter (learn more).
It can also be obtained by calling EntityCommands::id or EntityMut::id.
fn setup(mut commands: Commands) {
// Calling `spawn` returns `EntityCommands`.
let entity = commands.spawn(SomeComponent).id();
}
fn exclusive_system(world: &mut World) {
// Calling `spawn` returns `EntityMut`.
let entity = world.spawn(SomeComponent).id();
}It can be used to refer to a specific entity to apply EntityCommands, or to call Query::get (or similar methods) to access its components.
fn dispose_expired_food(mut commands: Commands, query: Query<Entity, With<Expired>>) {
for food_entity in &query {
commands.entity(food_entity).despawn();
}
}Implementations§
Source§impl Entity
impl Entity
Sourcepub const fn from_raw(index: u32) -> Entity
pub const fn from_raw(index: u32) -> Entity
Creates a new entity reference with the specified index and a generation of 0.
§Note
Spawning a specific entity value is rarely the right choice. Most apps should favor
Commands::spawn. This method should generally
only be used for sharing entities across apps, and only when they have a scheme
worked out to share an index space (which doesn’t happen by default).
In general, one should not try to synchronize the ECS by attempting to ensure that
Entity lines up between instances, but instead insert a secondary identifier as
a component.
There are still some use cases where it might be appropriate to use this function externally.
§Examples
Initializing a collection (e.g. array or Vec) with a known size:
// Create a new array of size 10 and initialize it with (invalid) entities.
let mut entities: [Entity; 10] = [Entity::from_raw(0); 10];
// ... replace the entities with valid ones.Deriving Reflect for a component that has an Entity field:
#[derive(Reflect, Component)]
#[reflect(Component)]
pub struct MyStruct {
pub entity: Entity,
}
impl FromWorld for MyStruct {
fn from_world(_world: &mut World) -> Self {
Self {
entity: Entity::from_raw(u32::MAX),
}
}
}Sourcepub const fn to_bits(self) -> u64
pub const fn to_bits(self) -> u64
Convert to a form convenient for passing outside of rust.
Only useful for identifying entities within the same instance of an application. Do not use for serialization between runs.
No particular structure is guaranteed for the returned bits.
Sourcepub const fn from_bits(bits: u64) -> Entity
pub const fn from_bits(bits: u64) -> Entity
Reconstruct an Entity previously destructured with Entity::to_bits.
Only useful when applied to results from to_bits in the same instance of an application.
Sourcepub const fn index(self) -> u32
pub const fn index(self) -> u32
Return a transiently unique identifier.
No two simultaneously-live entities share the same index, but dead entities’ indices may collide with both live and dead entities. Useful for compactly representing entities within a specific snapshot of the world, such as when serializing.
Sourcepub const fn generation(self) -> u32
pub const fn generation(self) -> u32
Returns the generation of this Entity’s index. The generation is incremented each time an entity with a given index is despawned. This serves as a “count” of the number of times a given index has been reused (index, generation) pairs uniquely identify a given Entity.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Entity
impl<'de> Deserialize<'de> for Entity
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Entity, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Entity, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl FromReflect for Entity
impl FromReflect for Entity
Source§impl GetTypeRegistration for Entity
impl GetTypeRegistration for Entity
Source§impl Ord for Entity
impl Ord for Entity
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Entity
impl PartialOrd for Entity
Source§impl Reflect for Entity
impl Reflect for Entity
Source§fn get_type_info(&self) -> &'static TypeInfo
fn get_type_info(&self) -> &'static TypeInfo
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut dyn Any.Source§fn into_reflect(self: Box<Entity>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Entity>) -> Box<dyn Reflect>
Source§fn as_reflect(&self) -> &(dyn Reflect + 'static)
fn as_reflect(&self) -> &(dyn Reflect + 'static)
Source§fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
Source§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect trait object. Read moreSource§fn apply(&mut self, value: &(dyn Reflect + 'static))
fn apply(&mut self, value: &(dyn Reflect + 'static))
Source§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Entity>) -> ReflectOwned
fn reflect_owned(self: Box<Entity>) -> ReflectOwned
Source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
Source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
Source§impl Serialize for Entity
impl Serialize for Entity
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Source§impl SparseSetIndex for Entity
impl SparseSetIndex for Entity
fn sparse_set_index(&self) -> usize
fn get_sparse_set_index(value: usize) -> Entity
Source§impl WorldQuery for Entity
SAFETY: no component or archetype access
impl WorldQuery for Entity
SAFETY: no component or archetype access
Source§const IS_DENSE: bool = true
const IS_DENSE: bool = true
WorldQuery::set_table must be used before
WorldQuery::fetch can be called for iterators. If this returns false,
WorldQuery::set_archetype must be used before WorldQuery::fetch can be called for
iterators.Source§const IS_ARCHETYPAL: bool = true
const IS_ARCHETYPAL: bool = true
Source§type Fetch<'w> = ()
type Fetch<'w> = ()
WorldQuery to fetch Self::ItemSource§type Item<'w> = Entity
type Item<'w> = Entity
WorldQuerySource§type ReadOnly = Entity
type ReadOnly = Entity
WorldQuery, which satisfies the ReadOnlyWorldQuery trait.Source§type State = ()
type State = ()
Self::Fetch. This will be cached inside QueryState,
so it is best to move as much data / computation here as possible to reduce the cost of
constructing Self::Fetch.Source§fn shrink<'wlong, 'wshort>(
item: <Entity as WorldQuery>::Item<'wlong>,
) -> <Entity as WorldQuery>::Item<'wshort>where
'wlong: 'wshort,
fn shrink<'wlong, 'wshort>(
item: <Entity as WorldQuery>::Item<'wlong>,
) -> <Entity as WorldQuery>::Item<'wshort>where
'wlong: 'wshort,
Source§unsafe fn init_fetch<'w>(
_world: &'w World,
_state: &<Entity as WorldQuery>::State,
_last_change_tick: u32,
_change_tick: u32,
) -> <Entity as WorldQuery>::Fetch<'w>
unsafe fn init_fetch<'w>( _world: &'w World, _state: &<Entity as WorldQuery>::State, _last_change_tick: u32, _change_tick: u32, ) -> <Entity as WorldQuery>::Fetch<'w>
Source§unsafe fn clone_fetch<'w>(
_fetch: &<Entity as WorldQuery>::Fetch<'w>,
) -> <Entity as WorldQuery>::Fetch<'w>
unsafe fn clone_fetch<'w>( _fetch: &<Entity as WorldQuery>::Fetch<'w>, ) -> <Entity as WorldQuery>::Fetch<'w>
Self: ReadOnlyWorldQuery holds. Read moreSource§unsafe fn set_archetype<'w>(
_fetch: &mut <Entity as WorldQuery>::Fetch<'w>,
_state: &<Entity as WorldQuery>::State,
_archetype: &'w Archetype,
_table: &Table,
)
unsafe fn set_archetype<'w>( _fetch: &mut <Entity as WorldQuery>::Fetch<'w>, _state: &<Entity as WorldQuery>::State, _archetype: &'w Archetype, _table: &Table, )
Archetype. This will always be called on
archetypes that match this WorldQuery. Read moreSource§unsafe fn set_table<'w>(
_fetch: &mut <Entity as WorldQuery>::Fetch<'w>,
_state: &<Entity as WorldQuery>::State,
_table: &'w Table,
)
unsafe fn set_table<'w>( _fetch: &mut <Entity as WorldQuery>::Fetch<'w>, _state: &<Entity as WorldQuery>::State, _table: &'w Table, )
Table. This will always be called on tables
that match this WorldQuery. Read moreSource§unsafe fn fetch<'w>(
_fetch: &mut <Entity as WorldQuery>::Fetch<'w>,
entity: Entity,
_table_row: usize,
) -> <Entity as WorldQuery>::Item<'w>
unsafe fn fetch<'w>( _fetch: &mut <Entity as WorldQuery>::Fetch<'w>, entity: Entity, _table_row: usize, ) -> <Entity as WorldQuery>::Item<'w>
Self::Item for either the given entity in the current Table,
or for the given entity in the current Archetype. This must always be called after
WorldQuery::set_table with a table_row in the range of the current Table or after
WorldQuery::set_archetype with a entity in the current archetype. Read morefn update_component_access( _state: &<Entity as WorldQuery>::State, _access: &mut FilteredAccess<ComponentId>, )
fn update_archetype_component_access( _state: &<Entity as WorldQuery>::State, _archetype: &Archetype, _access: &mut Access<ArchetypeComponentId>, )
fn init_state(_world: &mut World)
fn matches_component_set( _state: &<Entity as WorldQuery>::State, _set_contains_id: &impl Fn(ComponentId) -> bool, ) -> bool
impl Copy for Entity
impl Eq for Entity
impl ReadOnlyWorldQuery for Entity
SAFETY: access is read only
impl StructuralPartialEq for Entity
Auto Trait Implementations§
impl Freeze for Entity
impl RefUnwindSafe for Entity
impl Send for Entity
impl Sync for Entity
impl Unpin for Entity
impl UnsafeUnpin for Entity
impl UnwindSafe for Entity
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
Source§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> GetPath for Twhere
T: Reflect,
impl<T> GetPath for Twhere
T: Reflect,
Source§fn path<'r, 'p>(
&'r self,
path: &'p str,
) -> Result<&'r (dyn Reflect + 'static), ReflectPathError<'p>>
fn path<'r, 'p>( &'r self, path: &'p str, ) -> Result<&'r (dyn Reflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn path_mut<'r, 'p>(
&'r mut self,
path: &'p str,
) -> Result<&'r mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn path_mut<'r, 'p>( &'r mut self, path: &'p str, ) -> Result<&'r mut (dyn Reflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn get_path<'r, 'p, T>(
&'r self,
path: &'p str,
) -> Result<&'r T, ReflectPathError<'p>>where
T: Reflect,
fn get_path<'r, 'p, T>(
&'r self,
path: &'p str,
) -> Result<&'r T, ReflectPathError<'p>>where
T: Reflect,
path.Source§fn get_path_mut<'r, 'p, T>(
&'r mut self,
path: &'p str,
) -> Result<&'r mut T, ReflectPathError<'p>>where
T: Reflect,
fn get_path_mut<'r, 'p, T>(
&'r mut self,
path: &'p str,
) -> Result<&'r mut T, ReflectPathError<'p>>where
T: Reflect,
path.