bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Add Reflect derive to Events and contained types

Open alice-i-cecile opened this issue 9 months ago • 1 comments

Objective

The Events containerr should be reflectable, in order to make dev tools that examine its state more useful.

Fixes #13148.

Solution

  • Add a Reflect derive to Events, gated behind the bevy_reflect feature
  • Add Reflect to the contained types to make everything compile.

alice-i-cecile avatar Apr 30 '24 17:04 alice-i-cecile

@alice-i-cecile thanks, its works!

use bevy::prelude::*;

#[derive(Component, Reflect, Default)]
struct Position;

#[derive(Event, Reflect, Default)]
struct MoveEvent;

#[derive(Resource, Reflect, Default)]
struct Houses;

fn main() {
    App::new()
        // Position
        .register_type::<Position>()
        // MoveEvent
        .add_event::<MoveEvent>()
        .register_type::<MoveEvent>()
        // Houses
        .init_resource::<Houses>()
        .register_type::<Houses>()
        // Systems
        .add_systems(Startup, startup)
        .run();
}

fn startup(registry: Res<AppTypeRegistry>) {
    let registry = registry.read();
    for i in registry.iter() {
        println!("{:?}", i);
    }
}

output:

TypeRegistration { type_info: Struct(StructInfo { type_path: TypePathVtable { type_path: "bevy::Houses", short_type_path: "Houses", type_ident: Some("Houses"), crate_name: Some("bevy"), module_path: Some("bevy") }, type_id: TypeId { t: 262497429019092159181105962761250037597 }, fields: [], field_names: [], field_indices: {} }) }
TypeRegistration { type_info: Struct(StructInfo { type_path: TypePathVtable { type_path: "bevy::Position", short_type_path: "Position", type_ident: Some("Position"), crate_name: Some("bevy"), module_path: Some("bevy") }, type_id: TypeId { t: 79962707724654762771215367196927044087 }, fields: [], field_names: [], field_indices: {} }) }
TypeRegistration { type_info: Struct(StructInfo { type_path: TypePathVtable { type_path: "bevy::MoveEvent", short_type_path: "MoveEvent", type_ident: Some("MoveEvent"), crate_name: Some("bevy"), module_path: Some("bevy") }, type_id: TypeId { t: 175823575746073673475871982974340081787 }, fields: [], field_names: [], field_indices: {} }) }

ramon-bernardo avatar Apr 30 '24 21:04 ramon-bernardo

Looks good! I think the one thing we might want to consider adding is a method on App to more easily register these events and their resources into the type registry.

Agreed. Ideally we'd register the type during add_event, but without specialization that would require a Reflect (or equivalent) bound on Event. Ultimately this feels like a question for #13111.

alice-i-cecile avatar May 01 '24 15:05 alice-i-cecile