Skip to content

Commit

Permalink
Construct Box<dyn Reflect> from world for ReflectComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
SneakyBerry committed Mar 7, 2023
1 parent 6595086 commit c800e09
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/bevy_ecs/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct ReflectComponent(ReflectComponentFns);
/// world.
#[derive(Clone)]
pub struct ReflectComponentFns {
/// Function pointer implementing [`ReflectComponent::from_world()`].
pub from_world: fn(&mut World) -> Box<dyn Reflect>,
/// Function pointer implementing [`ReflectComponent::insert()`].
pub insert: fn(&mut EntityMut, &dyn Reflect),
/// Function pointer implementing [`ReflectComponent::apply()`].
Expand Down Expand Up @@ -79,6 +81,11 @@ impl ReflectComponentFns {
}

impl ReflectComponent {
/// Constructs default reflected [`Component`] from world using [`form_world()`](FromWorld::from_world).
pub fn from_world(&self, world: &mut World) -> Box<dyn Reflect> {
(self.0.from_world)(world)
}

/// Insert a reflected [`Component`] into the entity like [`insert()`](crate::world::EntityMut::insert).
pub fn insert(&self, entity: &mut EntityMut, component: &dyn Reflect) {
(self.0.insert)(entity, component);
Expand Down Expand Up @@ -170,8 +177,9 @@ impl ReflectComponent {
impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
fn from_type() -> Self {
ReflectComponent(ReflectComponentFns {
insert: |entity, reflected_component| {
let mut component = entity.world_scope(|world| C::from_world(world));
from_world: |world| Box::new(C::from_world(world)),
insert: |world, entity, reflected_component| {
let mut component = C::from_world(world);
component.apply(reflected_component);
entity.insert(component);
},
Expand Down

0 comments on commit c800e09

Please sign in to comment.