Skip to content

Commit 7ddb1c2

Browse files
committed
Construct the concrete value through Reflect rather than FromReflect
1 parent b7d2613 commit 7ddb1c2

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

crates/bevy_ecs/src/reflect/map_entities.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::{
33
entity::{DynEntityMapper, Entity, EntityHashMap, MapEntities, SceneEntityMapper},
44
world::World,
55
};
6-
use bevy_reflect::{FromReflect, FromType, Reflect};
6+
use bevy_reflect::{FromType, Reflect, TypeRegistry};
7+
8+
use super::from_reflect_with_fallback;
79

810
/// For a specific type of component, this maps any fields with values of type [`Entity`] to a new world.
911
/// Since a given `Entity` ID is only valid for the world it came from, when performing deserialization
@@ -12,7 +14,7 @@ use bevy_reflect::{FromReflect, FromType, Reflect};
1214
/// See [`SceneEntityMapper`] and [`MapEntities`] for more information.
1315
#[derive(Clone)]
1416
pub struct ReflectMapEntities {
15-
map_entities: fn(&mut dyn Reflect, &mut dyn DynEntityMapper),
17+
map_entities: fn(&mut World, &TypeRegistry, &mut dyn Reflect, &mut dyn DynEntityMapper),
1618
map_all_world_entities: fn(&mut World, &mut SceneEntityMapper),
1719
map_world_entities: fn(&mut World, &mut SceneEntityMapper, &[Entity]),
1820
}
@@ -26,8 +28,14 @@ impl ReflectMapEntities {
2628
/// entities in a scene, if this is used on a component before ensuring that
2729
/// all entities in the scene have been allocated, a new mapping will be created
2830
/// with a "dead" entity.
29-
pub fn map_entities(&self, component: &mut dyn Reflect, mapper: &mut dyn DynEntityMapper) {
30-
(self.map_entities)(component, mapper);
31+
pub fn map_entities(
32+
&self,
33+
world: &mut World,
34+
type_registry: &TypeRegistry,
35+
component: &mut dyn Reflect,
36+
mapper: &mut dyn DynEntityMapper,
37+
) {
38+
(self.map_entities)(world, type_registry, component, mapper);
3139
}
3240

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

70-
impl<C: Component + MapEntities + FromReflect> FromType<C> for ReflectMapEntities {
78+
impl<C: Reflect + Component + MapEntities> FromType<C> for ReflectMapEntities {
7179
fn from_type() -> Self {
7280
ReflectMapEntities {
73-
map_entities: |component, mut entity_mapper| {
74-
let mut concrete = C::from_reflect(&*component).unwrap();
81+
map_entities: |world, type_registry, component, mut entity_mapper| {
82+
let mut concrete = from_reflect_with_fallback::<C>(component, world, type_registry);
7583
concrete.map_entities(&mut entity_mapper);
7684
component.apply(&concrete);
7785
},

crates/bevy_scene/src/dynamic_scene.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ impl DynamicScene {
110110
// If this component references entities in the scene, update
111111
// them to the entities in the world.
112112
if let Some(map_entities) = registration.data::<ReflectMapEntities>() {
113-
SceneEntityMapper::world_scope(entity_map, world, |_, mapper| {
114-
map_entities.map_entities(&mut *component, mapper);
113+
SceneEntityMapper::world_scope(entity_map, world, |world, mapper| {
114+
map_entities.map_entities(world, &type_registry, &mut *component, mapper);
115115
});
116116
}
117117

crates/bevy_scene/src/scene.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ impl Scene {
124124
// If this component references entities in the scene,
125125
// update them to the entities in the world.
126126
if let Some(map_entities) = registration.data::<ReflectMapEntities>() {
127-
SceneEntityMapper::world_scope(entity_map, world, |_, mapper| {
128-
map_entities.map_entities(&mut *component, mapper);
127+
SceneEntityMapper::world_scope(entity_map, world, |world, mapper| {
128+
map_entities.map_entities(
129+
world,
130+
&type_registry,
131+
&mut *component,
132+
mapper,
133+
);
129134
});
130135
}
131136

0 commit comments

Comments
 (0)