Trait bevy_ecs::entity::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).
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§
sourcefn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M)
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M)
Object Safety§
This trait is not object safe.