bevy/crates/bevy_reflect/src
Jakob Hellermann 2b6e67f4cb add #[reflect(Default)] to create default value for reflected types (#3733)
### Problem
It currently isn't possible to construct the default value of a reflected type. Because of that, it isn't possible to use `add_component` of `ReflectComponent` to add a new component to an entity because you can't know what the initial value should be.

### Solution

1. add `ReflectDefault` type
```rust
#[derive(Clone)]
pub struct ReflectDefault {
    default: fn() -> Box<dyn Reflect>,
}

impl ReflectDefault {
    pub fn default(&self) -> Box<dyn Reflect> {
        (self.default)()
    }
}

impl<T: Reflect + Default> FromType<T> for ReflectDefault {
    fn from_type() -> Self {
        ReflectDefault {
            default: || Box::new(T::default()),
        }
    }
}
```

2. add `#[reflect(Default)]` to all component types that implement `Default` and are user facing (so not `ComputedSize`, `CubemapVisibleEntities` etc.)



This makes it possible to add the default value of a component to an entity without any compile-time information:

```rust
fn main() {
    let mut app = App::new();
    app.register_type::<Camera>();

    let type_registry = app.world.get_resource::<TypeRegistry>().unwrap();
    let type_registry = type_registry.read();

    let camera_registration = type_registry.get(std::any::TypeId::of::<Camera>()).unwrap();
    let reflect_default = camera_registration.data::<ReflectDefault>().unwrap();
    let reflect_component = camera_registration
        .data::<ReflectComponent>()
        .unwrap()
        .clone();

    let default = reflect_default.default();

    drop(type_registry);

    let entity = app.world.spawn().id();
    reflect_component.add_component(&mut app.world, entity, &*default);

    let camera = app.world.entity(entity).get::<Camera>().unwrap();
    dbg!(&camera);
}
```

### Open questions
- should we have `ReflectDefault` or `ReflectFromWorld` or both?
2022-05-03 19:20:13 +00:00
..
impls bevy_reflect: Added PartialEq to reflected f32 & f64 (#4217) 2022-04-26 19:41:26 +00:00
serde Avoid some format! into immediate format! (#2913) 2021-10-06 18:34:33 +00:00
lib.rs add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
list.rs bevy_reflect: IntoIter for DynamicList and DynamicMap (#4108) 2022-04-26 00:17:38 +00:00
map.rs bevy_reflect: IntoIter for DynamicList and DynamicMap (#4108) 2022-04-26 00:17:38 +00:00
path.rs document more of bevy_reflect (#3655) 2022-01-14 19:09:44 +00:00
reflect.rs bevy_reflect: Add as_reflect and as_reflect_mut (#4350) 2022-04-25 13:54:48 +00:00
std_traits.rs add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
struct_trait.rs bevy_reflect: Add as_reflect and as_reflect_mut (#4350) 2022-04-25 13:54:48 +00:00
tuple.rs bevy_reflect: Add GetTypeRegistration impl for reflected tuples (#4226) 2022-05-02 18:04:48 +00:00
tuple_struct.rs bevy_reflect: Add as_reflect and as_reflect_mut (#4350) 2022-04-25 13:54:48 +00:00
type_registry.rs Re-enable test_property_type_registration() (#4419) 2022-04-05 18:34:27 +00:00
type_uuid.rs re-enable #[derive(TypeUuid)] for generics (#4118) 2022-04-26 19:41:25 +00:00