jakobhellermann / bevy-inspector-egui

Inspector plugin for the bevy game engine
Apache License 2.0
1.12k stars 166 forks source link

"You should register ReflectDefault for all fields" error message is unclear #116

Closed mwbryant closed 1 year ago

mwbryant commented 1 year ago

I have a component that has an array of optional enums as one of it's fields. When I try to set the value for one of these from None to Some using the inspector it gives the message: core::option::Option<project_name::Item> has unconstructable variants: Some, you should register ReflectDefault for all fields.

How am I supposed to do what this message is telling me? ReflectDefault appears to be a struct in bevy but their docs are unhelpful.

Here is the component in question by the way, both of these are registered types with the app:

#[derive(Default, Component, Reflect)]
#[reflect(Component)]
pub struct Belt {
    connection: Connection,
    near_items: [Option<Item>; BELT_SIZE],
    far_items: [Option<Item>; BELT_SIZE],
}

#[derive(Component, Default, FromReflect, Reflect, Clone, Copy)]
pub enum Item {
    #[default]
    Ore,
    Coal,
}
jakobhellermann commented 1 year ago

When the inspector sets the value to Some(item) it needs to have a default value for that item.

For that, you need to tell the type registry that item implements default, which you can do using


#[derive(Reflect, Default)]
#[reflect(Default)]
enum Item {}
mwbryant commented 1 year ago

Interesting! That solved the problem. It might be worth adding "#[reflect(Default)]` to the error message as that was hard to track down for me.