diff --git a/crates/bevy_component_registry/src/component_registry.rs b/crates/bevy_component_registry/src/component_registry.rs index f139e9053a..d28ed1fc7c 100644 --- a/crates/bevy_component_registry/src/component_registry.rs +++ b/crates/bevy_component_registry/src/component_registry.rs @@ -1,12 +1,13 @@ use bevy_property::{Properties, Property, PropertyTypeRegistry}; use legion::{ - prelude::{Entity, World}, + prelude::{Entity, World, Resources}, storage::{Component, ComponentResourceSet, ComponentTypeId}, }; use std::{ collections::HashMap, sync::{Arc, RwLock}, }; +use bevy_app::FromResources; #[derive(Clone, Default)] pub struct PropertyTypeRegistryContext { @@ -28,7 +29,7 @@ pub struct ComponentRegistry { impl ComponentRegistry { pub fn register(&mut self) where - T: Properties + Component + Default, + T: Properties + Component + FromResources, { let registration = ComponentRegistration::of::(); self.short_names @@ -58,18 +59,18 @@ impl ComponentRegistry { #[derive(Clone)] pub struct ComponentRegistration { pub ty: ComponentTypeId, - pub component_add_fn: fn(&mut World, Entity, &dyn Property), + pub component_add_fn: fn(&mut World, resources: &Resources, Entity, &dyn Property), pub component_properties_fn: fn(&ComponentResourceSet, usize) -> &dyn Properties, pub short_name: &'static str, } impl ComponentRegistration { - pub fn of() -> Self { + pub fn of() -> Self { let ty = ComponentTypeId::of::(); Self { ty, - component_add_fn: |world: &mut World, entity: Entity, property: &dyn Property| { - let mut component = T::default(); + component_add_fn: |world: &mut World, resources: &Resources, entity: Entity, property: &dyn Property| { + let mut component = T::from_resources(resources); component.apply(property); world.add_component(entity, component).unwrap(); }, diff --git a/crates/bevy_component_registry/src/register_component.rs b/crates/bevy_component_registry/src/register_component.rs index 0bdbb905eb..0280b24927 100644 --- a/crates/bevy_component_registry/src/register_component.rs +++ b/crates/bevy_component_registry/src/register_component.rs @@ -3,12 +3,12 @@ use bevy_property::{Property, Properties}; use legion::storage::Component; use serde::Deserialize; use crate::{PropertyTypeRegistryContext, ComponentRegistryContext}; -use bevy_app::AppBuilder; +use bevy_app::{FromResources, AppBuilder}; pub trait RegisterComponent { fn register_component(&mut self) -> &mut Self where - T: Properties + Component + Default; + T: Properties + Component + FromResources; fn register_property_type(&mut self) -> &mut Self where T: Property + for<'de> Deserialize<'de>; @@ -17,7 +17,7 @@ pub trait RegisterComponent { impl RegisterComponent for AppBuilder { fn register_component(&mut self) -> &mut Self where - T: Properties + Component + Default, + T: Properties + Component + FromResources, { { let registry_context = self diff --git a/crates/bevy_scene/src/scene.rs b/crates/bevy_scene/src/scene.rs index ce201383b5..117b55de02 100644 --- a/crates/bevy_scene/src/scene.rs +++ b/crates/bevy_scene/src/scene.rs @@ -1,7 +1,7 @@ use anyhow::Result; use bevy_component_registry::ComponentRegistry; use bevy_property::DynamicProperties; -use legion::prelude::World; +use legion::prelude::{Resources, World}; use serde::Serialize; use std::num::Wrapping; use thiserror::Error; @@ -66,6 +66,7 @@ impl Scene { pub fn add_to_world( &self, world: &mut World, + resources: &Resources, component_registry: &ComponentRegistry, ) -> Result<(), SceneAddError> { world.entity_allocator.push_next_ids( @@ -82,7 +83,7 @@ impl Scene { .ok_or_else(|| SceneAddError::UnregisteredComponent { type_name: component.type_name.to_string(), })?; - (component_registration.component_add_fn)(world, entity, component); + (component_registration.component_add_fn)(world, resources, entity, component); } } diff --git a/examples/scene/load_scene.rs b/examples/scene/load_scene.rs index f96e5b3850..5060fd3f11 100644 --- a/examples/scene/load_scene.rs +++ b/examples/scene/load_scene.rs @@ -34,7 +34,7 @@ fn load_scene(world: &mut World, resources: &mut Resources) { .unwrap(); let scene = scenes.get(&scene_handle).unwrap(); scene - .add_to_world(world, &component_registry.value.read().unwrap()) + .add_to_world(world, resources, &component_registry.value.read().unwrap()) .unwrap(); }