Entity
Opaque Type. 🔒
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.
These identifiers are only valid on the
World
it's sourced from. Attempting to use anEntity
to fetch entity components or metadata from a different world will either fail or return unexpected results.Stability warning
For all intents and purposes,
Entity
should be treated as an opaque identifier. The internal bit representation is liable to change from release to release as are the behaviors or performance characteristics of any of its trait implementations (i.e.Ord
,Hash
, etc.). This means that changes inEntity
's representation, though made readable through various functions on the type, are not considered breaking changes under SemVer.In particular, directly serializing with
Serialize
andDeserialize
make zero guarantee of long term wire format compatibility. Changes in behavior will cause serializedEntity
values persisted to long term storage (i.e. disk, databases, etc.) will fail to deserialize upon being updated.Usage
This data type is returned by iterating a
Query
that hasEntity
as part of its query fetch type parameter (learn more). It can also be obtained by callingEntityCommands::id
orEntityWorldMut::id
.# use bevy_ecs::prelude::*; # #[derive(Component)] # struct SomeComponent; fn setup(mut commands: Commands) { // Calling `spawn` returns `EntityCommands`. let entity = commands.spawn(SomeComponent).id(); } fn exclusive_system(world: &mut World) { // Calling `spawn` returns `EntityWorldMut`. let entity = world.spawn(SomeComponent).id(); } # # bevy_ecs::system::assert_is_system(setup); # bevy_ecs::system::assert_is_system(exclusive_system);
It can be used to refer to a specific entity to apply
EntityCommands
, or to callQuery::get
(or similar methods) to access its components.# use bevy_ecs::prelude::*; # # #[derive(Component)] # struct Expired; # fn dispose_expired_food(mut commands: Commands, query: Query<Entity, With<Expired>>) { for food_entity in &query { commands.entity(food_entity).despawn(); } } # # bevy_ecs::system::assert_is_system(dispose_expired_food);