bevy_ecs::entity

Trait MapEntities

Source
pub trait MapEntities {
    // Required method
    fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M);
}
Expand description

Operation to map all contained Entity fields in a type to new values.

As entity IDs are valid only for the World they’re sourced from, using Entity as references in components copied from another world will be invalid. This trait allows defining custom mappings for these references via EntityMappers, which inject the entity mapping strategy between your MapEntities type and the current world (usually by using an EntityHashMap<Entity> between source entities and entities in the current world).

This trait is similar to VisitEntitiesMut. They differ in that VisitEntitiesMut operates on &mut Entity and allows for in-place modification, while this trait makes no assumption that such in-place modification is occurring, which is impossible for types such as HashSet<Entity> and EntityHashMap which must be rebuilt when their contained Entitys are remapped.

Implementing this trait correctly is required for properly loading components with entity references from scenes.

§Example

use bevy_ecs::prelude::*;
use bevy_ecs::entity::MapEntities;

#[derive(Component)]
struct Spring {
    a: Entity,
    b: Entity,
}

impl MapEntities for Spring {
    fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
        self.a = entity_mapper.map_entity(self.a);
        self.b = entity_mapper.map_entity(self.b);
    }
}

Required Methods§

Source

fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M)

Updates all Entity references stored inside using entity_mapper.

Implementors should look up any and all Entity values stored within self and update them to the mapped values via entity_mapper.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§