Question: how to deal with optional fields of a component?
AntonZelenin opened this issue · comments
I'm trying to use a couple of components:
#[derive(Component, Schematic, Reflect)]
#[reflect(Schematic)]
pub enum Direction {
Up,
Down,
Left,
Right,
}
#[derive(Component, Schematic, Reflect)]
#[reflect(Schematic)]
pub struct MovementDirection {
pub x_axis: Option<Direction>,
pub y_axis: Option<Direction>,
}
but I get an error from proto no registration found for type core::option::Option<expeditions::core::controls::components::Direction>
when I try to spawn a player that has MovementDirection. How can I use a component with another component that is optional? It is working with standard types like String, but not with my components.
Unfortunately with bevy_reflect, you need to manually monomorphize and register generic types. Bevy does this for a few types (i.e. Option<String>
) but you have to do the same with your own types (until something like bevyengine/bevy#5781 is merged).
So in your case,you could do
app.register_type::<Option<Direction>>();
Note
If the derive macro doesn't automatically pick up your Option
, you can add #[schematic(optional)]
to your field.
Unfortunately with bevy_reflect, you need to manually monomorphize and register generic types. Bevy does this for a few types (i.e.
Option<String>
) but you have to do the same with your own types (until something like bevyengine/bevy#5781 is merged).So in your case,you could do
app.register_type::<Option<Direction>>();Note If the derive macro doesn't automatically pick up your
Option
, you can add#[schematic(optional)]
to your field.
Oh, great, it works! Thank you :)