Skip to content

Commit

Permalink
Construct the concrete value through Reflect rather than FromReflect
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobsonchase committed Jul 31, 2024
1 parent b7d2613 commit 7ddb1c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
22 changes: 15 additions & 7 deletions crates/bevy_ecs/src/reflect/map_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::{
entity::{DynEntityMapper, Entity, EntityHashMap, MapEntities, SceneEntityMapper},
world::World,
};
use bevy_reflect::{FromReflect, FromType, Reflect};
use bevy_reflect::{FromType, Reflect, TypeRegistry};

use super::from_reflect_with_fallback;

/// For a specific type of component, this maps any fields with values of type [`Entity`] to a new world.
/// Since a given `Entity` ID is only valid for the world it came from, when performing deserialization
Expand All @@ -12,7 +14,7 @@ use bevy_reflect::{FromReflect, FromType, Reflect};
/// See [`SceneEntityMapper`] and [`MapEntities`] for more information.
#[derive(Clone)]
pub struct ReflectMapEntities {
map_entities: fn(&mut dyn Reflect, &mut dyn DynEntityMapper),
map_entities: fn(&mut World, &TypeRegistry, &mut dyn Reflect, &mut dyn DynEntityMapper),
map_all_world_entities: fn(&mut World, &mut SceneEntityMapper),
map_world_entities: fn(&mut World, &mut SceneEntityMapper, &[Entity]),
}
Expand All @@ -26,8 +28,14 @@ impl ReflectMapEntities {
/// entities in a scene, if this is used on a component before ensuring that
/// all entities in the scene have been allocated, a new mapping will be created
/// with a "dead" entity.
pub fn map_entities(&self, component: &mut dyn Reflect, mapper: &mut dyn DynEntityMapper) {
(self.map_entities)(component, mapper);
pub fn map_entities(
&self,
world: &mut World,
type_registry: &TypeRegistry,
component: &mut dyn Reflect,
mapper: &mut dyn DynEntityMapper,
) {
(self.map_entities)(world, type_registry, component, mapper);
}

/// A general method for applying [`MapEntities`] behavior to all elements in an [`EntityHashMap<Entity>`].
Expand Down Expand Up @@ -67,11 +75,11 @@ impl ReflectMapEntities {
}
}

impl<C: Component + MapEntities + FromReflect> FromType<C> for ReflectMapEntities {
impl<C: Reflect + Component + MapEntities> FromType<C> for ReflectMapEntities {
fn from_type() -> Self {
ReflectMapEntities {
map_entities: |component, mut entity_mapper| {
let mut concrete = C::from_reflect(&*component).unwrap();
map_entities: |world, type_registry, component, mut entity_mapper| {
let mut concrete = from_reflect_with_fallback::<C>(component, world, type_registry);
concrete.map_entities(&mut entity_mapper);
component.apply(&concrete);
},
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl DynamicScene {
// If this component references entities in the scene, update
// them to the entities in the world.
if let Some(map_entities) = registration.data::<ReflectMapEntities>() {
SceneEntityMapper::world_scope(entity_map, world, |_, mapper| {
map_entities.map_entities(&mut *component, mapper);
SceneEntityMapper::world_scope(entity_map, world, |world, mapper| {
map_entities.map_entities(world, &type_registry, &mut *component, mapper);
});
}

Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_scene/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ impl Scene {
// If this component references entities in the scene,
// update them to the entities in the world.
if let Some(map_entities) = registration.data::<ReflectMapEntities>() {
SceneEntityMapper::world_scope(entity_map, world, |_, mapper| {
map_entities.map_entities(&mut *component, mapper);
SceneEntityMapper::world_scope(entity_map, world, |world, mapper| {
map_entities.map_entities(
world,
&type_registry,
&mut *component,
mapper,
);
});
}

Expand Down

0 comments on commit 7ddb1c2

Please sign in to comment.