logo

Struct bevy::ecs::entity::Entity[]

pub struct Entity { /* fields omitted */ }
Expand description

Lightweight unique ID of an entity.

Obtained from World::spawn, typically via Commands::spawn. Can be stored to refer to an entity in the future.

Entity can be a part of a query, e.g. Query<(Entity, &MyComponent)>. Components of a specific entity can be accessed using Query::get and related methods.

Implementations

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),
        }
    }
}

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.

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.

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Performs the conversion.

Creates a clone of a reflected value, converting it to a concrete type if it was a dynamic types (e.g. DynamicStruct)

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Returns a hash of the value (which includes the type) if hashing is supported. Otherwise None will be returned. Read more

Returns a “partial equal” comparison result if comparison is supported. Otherwise None will be returned. Read more

Returns a serializable value, if serialization is supported. Otherwise None will be returned. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Convert 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. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more