Trait MapEntities

Source
pub trait MapEntities {
    // Required method
    fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E);
}
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).

Components use Component::map_entities to map entities in the context of scenes and entity cloning, which generally uses MapEntities internally to map each field (see those docs for usage).

§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.get_mapped(self.a);
        self.b = entity_mapper.get_mapped(self.b);
    }
}

Required Methods§

Source

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

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.

Implementations on Foreign Types§

Source§

impl MapEntities for Option<Entity>

Source§

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

Source§

impl MapEntities for VecDeque<Entity>

Source§

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

Source§

impl MapEntities for Vec<Entity>

Source§

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

Source§

impl<A: Array<Item = Entity>> MapEntities for SmallVec<A>

Source§

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

Source§

impl<S: BuildHasher + Default> MapEntities for HashSet<Entity, S>

Source§

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

Implementors§