pub struct Entity { /* private fields */ }Expand description
Lightweight identifier of an entity.
The identifier is implemented using a generational index: a combination of an ID 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().id();
}
fn exclusive_system(world: &mut World) {
// Calling `spawn` returns `EntityMut`.
let entity = world.spawn().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 fn from_raw(id: u32) -> Entity
pub fn from_raw(id: u32) -> Entity
Creates a new entity reference with the specified id 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 ID 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 fn to_bits(self) -> u64
pub 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 fn from_bits(bits: u64) -> Entity
pub 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 fn id(self) -> u32
pub fn id(self) -> u32
Return a transiently unique identifier.
No two simultaneously-live entities share the same ID, but dead entities’ IDs 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 fn generation(self) -> u32
pub fn generation(self) -> u32
Returns the generation of this Entity’s id. The generation is incremented each time an entity with a given id is despawned. This serves as a “count” of the number of times a given id has been reused (id, 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
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 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_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§impl<'w> WorldQueryGats<'w> for Entity
impl<'w> WorldQueryGats<'w> for Entity
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 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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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.