bevy/crates
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
..
bevy_animation Make AnimationClip::duration return value instead of reference (#4617) 2022-04-27 23:44:06 +00:00
bevy_app Tidy up PluginGroupBuilder by moving Plugin index retrieval to it's own function (#4446) 2022-05-02 13:20:54 +00:00
bevy_asset Add file metadata to AssetIo (#2123) 2022-05-02 18:04:47 +00:00
bevy_audio Converted exclusive systems to parallel systems wherever possible (#2774) 2022-04-25 14:32:56 +00:00
bevy_core add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_core_pipeline Do not create nor execute render passes which have no phase items to draw (#4643) 2022-05-02 20:22:30 +00:00
bevy_crevice Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_derive Decouple some dependencies (#3886) 2022-04-27 19:08:11 +00:00
bevy_diagnostic Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_dylib Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_dynamic_plugin Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_ecs Add get_change_ticks method to EntityRef and EntityMut (#2539) 2022-05-02 18:44:58 +00:00
bevy_ecs_compile_fail_tests Introduce tests for derive(WorldQuery) (#4625) 2022-04-28 21:06:20 +00:00
bevy_gilrs Change gamepad.rs tuples to normal structs (#4519) 2022-05-02 13:20:55 +00:00
bevy_gltf Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_hierarchy Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_input Change gamepad.rs tuples to normal structs (#4519) 2022-05-02 13:20:55 +00:00
bevy_internal Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_log can specify chrome tracing file (#4618) 2022-05-03 15:35:04 +00:00
bevy_macro_utils Decouple some dependencies (#3886) 2022-04-27 19:08:11 +00:00
bevy_math Document bevy_math (#4591) 2022-04-26 18:23:29 +00:00
bevy_pbr add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_reflect add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_render add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_scene Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_sprite Filter material handles on extraction (#4178) 2022-05-03 18:28:04 +00:00
bevy_tasks Remove unused CountdownEvent (#4290) 2022-04-26 21:20:12 +00:00
bevy_text add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_transform add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_ui add #[reflect(Default)] to create default value for reflected types (#3733) 2022-05-03 19:20:13 +00:00
bevy_utils Decouple some dependencies (#3886) 2022-04-27 19:08:11 +00:00
bevy_window Converted exclusive systems to parallel systems wherever possible (#2774) 2022-04-25 14:32:56 +00:00
bevy_winit Add infallible resource getters for WorldCell (#4104) 2022-04-25 23:19:13 +00:00