Trait bevy::ecs::entity::MapEntities

pub trait MapEntities {
    // Required method
    fn map_entities(
        &mut self,
        entity_map: &EntityMap
    ) -> Result<(), MapEntitiesError>;
}
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 EntityMap.

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

Example

use bevy_ecs::prelude::*;
use bevy_ecs::entity::{EntityMap, MapEntities, MapEntitiesError};

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

impl MapEntities for Spring {
    fn map_entities(&mut self, entity_map: &EntityMap) -> Result<(), MapEntitiesError> {
        self.a = entity_map.get(self.a)?;
        self.b = entity_map.get(self.b)?;
        Ok(())
    }
}

Required Methods§

fn map_entities( &mut self, entity_map: &EntityMap ) -> Result<(), MapEntitiesError>

Updates all Entity references stored inside using entity_map.

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

Implementors§