From e618426faa779662ec42a877867e023e39f9e9ec Mon Sep 17 00:00:00 2001 From: Charles Bournhonesque Date: Thu, 1 Feb 2024 11:11:32 -0500 Subject: [PATCH] Adding derive Reflect for tick structs (#11641) # Objective - Deriving `Reflect` for some public ChangeDetection/Tick structs in bevy_ecs --------- Co-authored-by: Charles Bournhonesque --- crates/bevy_core/src/lib.rs | 11 ++++++++++- crates/bevy_ecs/src/component.rs | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 5c669186d7..708b006d7d 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -21,6 +21,7 @@ pub mod prelude { } use bevy_app::prelude::*; +use bevy_ecs::component::{ComponentId, ComponentTicks, Tick}; use bevy_ecs::prelude::*; use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_utils::{Duration, HashSet, Instant, Uuid}; @@ -40,13 +41,21 @@ pub struct TypeRegistrationPlugin; impl Plugin for TypeRegistrationPlugin { fn build(&self, app: &mut App) { - app.register_type::().register_type::(); + app.register_type::(); + register_ecs_types(app); register_rust_types(app); register_math_types(app); } } +fn register_ecs_types(app: &mut App) { + app.register_type::() + .register_type::() + .register_type::() + .register_type::(); +} + fn register_rust_types(app: &mut App) { app.register_type::>() .register_type_data::, ReflectSerialize>() diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index e017ea4f54..cae48be957 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -10,6 +10,8 @@ use crate::{ }; pub use bevy_ecs_macros::Component; use bevy_ptr::{OwningPtr, UnsafeCellDeref}; +#[cfg(feature = "bevy_reflect")] +use bevy_reflect::Reflect; use std::cell::UnsafeCell; use std::{ alloc::Layout, @@ -287,6 +289,11 @@ impl ComponentInfo { /// from a `World` using [`World::component_id()`] or via [`Components::component_id()`]. Access /// to the `ComponentId` for a [`Resource`] is available via [`Components::resource_id()`]. #[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)] +#[cfg_attr( + feature = "bevy_reflect", + derive(Reflect), + reflect(Debug, Hash, PartialEq) +)] pub struct ComponentId(usize); impl ComponentId { @@ -670,6 +677,7 @@ impl Components { /// A value that tracks when a system ran relative to other systems. /// This is used to power change detection. #[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))] pub struct Tick { tick: u32, } @@ -763,6 +771,7 @@ impl<'a> TickCells<'a> { /// Records when a component was added and when it was last mutably dereferenced (or added). #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))] pub struct ComponentTicks { pub(crate) added: Tick, pub(crate) changed: Tick,