Skip to content

Commit

Permalink
Add Reflect derive to Events and contained types (bevyengine#13149)
Browse files Browse the repository at this point in the history
# Objective

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

Fixes bevyengine#13148.

## Solution

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

---------

Co-authored-by: Alice Cecile <[email protected]>
  • Loading branch information
alice-i-cecile and Alice Cecile authored May 1, 2024
1 parent dac66e8 commit b3ed0dd
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{
};
pub use bevy_ecs_macros::Event;
use bevy_ecs_macros::SystemSet;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use bevy_utils::detailed_trace;
use std::ops::{Deref, DerefMut};
use std::{
Expand All @@ -34,10 +36,12 @@ pub trait Event: Send + Sync + 'static {}
/// sent to the point it was processed. `EventId`s increase montonically by send order.
///
/// [`World`]: crate::world::World
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct EventId<E: Event> {
/// Uniquely identifies the event associated with this ID.
// This value corresponds to the order in which each event was added to the world.
pub id: usize,
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
_marker: PhantomData<E>,
}

Expand Down Expand Up @@ -93,6 +97,7 @@ impl<E: Event> Hash for EventId<E> {
}

#[derive(Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
struct EventInstance<E: Event> {
pub event_id: EventId<E>,
pub event: E,
Expand Down Expand Up @@ -171,6 +176,7 @@ struct EventInstance<E: Event> {
/// [Example usage standalone.](https://github.com/bevyengine/bevy/blob/latest/crates/bevy_ecs/examples/events.rs)
///
#[derive(Debug, Resource)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct Events<E: Event> {
/// Holds the oldest still active events.
/// Note that `a.start_event_count + a.len()` should always be equal to `events_b.start_event_count`.
Expand Down Expand Up @@ -393,6 +399,7 @@ impl<E: Event> Extend<E> for Events<E> {
}

#[derive(Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
struct EventSequence<E: Event> {
events: Vec<EventInstance<E>>,
start_event_count: usize,
Expand Down

0 comments on commit b3ed0dd

Please sign in to comment.