Trait bevy::ecs::entity::MapEntities

pub trait MapEntities {
    // Required method
    fn map_entities(&mut self, entity_mapper: &mut EntityMapper<'_>);
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 EntityHashMap<Entity, Entity>

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


use bevy_ecs::prelude::*;
use bevy_ecs::entity::{EntityMapper, MapEntities};

struct Spring {
    a: Entity,
    b: Entity,

impl MapEntities for Spring {
    fn map_entities(&mut self, entity_mapper: &mut EntityMapper) {
        self.a = entity_mapper.get_or_reserve(self.a);
        self.b = entity_mapper.get_or_reserve(self.b);

Required Methods§

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

Updates all Entity references stored inside using entity_mapper.

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