Skip to content

Exposes Observer's system's name #19611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
33 changes: 25 additions & 8 deletions crates/bevy_ecs/src/observer/runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{boxed::Box, vec};
use alloc::{borrow::Cow, boxed::Box, vec};
use core::any::Any;

use crate::{
Expand Down Expand Up @@ -194,7 +194,7 @@ pub type ObserverRunner = fn(DeferredWorld, ObserverTrigger, PtrMut, propagate:
pub struct Observer {
hook_on_add: ComponentHook,
error_handler: Option<ErrorHandler>,
system: Box<dyn Any + Send + Sync + 'static>,
system: Box<dyn AnyNamedSystem>,
pub(crate) descriptor: ObserverDescriptor,
pub(crate) last_trigger_id: u32,
pub(crate) despawned_watched_entities: u32,
Expand Down Expand Up @@ -232,7 +232,7 @@ impl Observer {
/// Creates a new [`Observer`] with custom runner, this is mostly used for dynamic event observer
pub fn with_dynamic_runner(runner: ObserverRunner) -> Self {
Self {
system: Box::new(|| {}),
system: Box::new(IntoSystem::into_system(|| {})),
descriptor: Default::default(),
hook_on_add: |mut world, hook_context| {
let default_error_handler = world.default_error_handler();
Expand Down Expand Up @@ -299,6 +299,11 @@ impl Observer {
pub fn descriptor(&self) -> &ObserverDescriptor {
&self.descriptor
}

/// Returns the name of the [`Observer`]'s system .
pub fn system_name(&self) -> Cow<'static, str> {
self.system.system_name()
}
}

impl Component for Observer {
Expand Down Expand Up @@ -364,7 +369,8 @@ fn observer_system_runner<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
// - observer was triggered so must have an `Observer` component.
// - observer cannot be dropped or mutated until after the system pointer is already dropped.
let system: *mut dyn ObserverSystem<E, B> = unsafe {
let system = state.system.downcast_mut::<S>().debug_checked_unwrap();
let system: &mut dyn Any = state.system.as_mut();
let system = system.downcast_mut::<S>().debug_checked_unwrap();
&mut *system
};

Expand Down Expand Up @@ -413,6 +419,16 @@ fn observer_system_runner<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
}
}

trait AnyNamedSystem: Any + Send + Sync + 'static {
fn system_name(&self) -> Cow<'static, str>;
}

impl<T: Any + System> AnyNamedSystem for T {
fn system_name(&self) -> Cow<'static, str> {
self.name()
}
}

/// A [`ComponentHook`] used by [`Observer`] to handle its [`on-add`](`crate::lifecycle::ComponentHooks::on_add`).
///
/// This function exists separate from [`Observer`] to allow [`Observer`] to have its type parameters
Expand All @@ -431,11 +447,12 @@ fn hook_on_add<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
B::component_ids(&mut world.components_registrator(), &mut |id| {
components.push(id);
});
if let Some(mut observe) = world.get_mut::<Observer>(entity) {
observe.descriptor.events.push(event_id);
observe.descriptor.components.extend(components);
if let Some(mut observer) = world.get_mut::<Observer>(entity) {
observer.descriptor.events.push(event_id);
observer.descriptor.components.extend(components);

let system: *mut dyn ObserverSystem<E, B> = observe.system.downcast_mut::<S>().unwrap();
let system: &mut dyn Any = observer.system.as_mut();
let system: *mut dyn ObserverSystem<E, B> = system.downcast_mut::<S>().unwrap();
// SAFETY: World reference is exclusive and initialize does not touch system, so references do not alias
unsafe {
(*system).initialize(world);
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_ecs/src/system/schedule_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ where
T: Send + Sync + 'static,
{
type In = ();

type Out = S::Out;

fn name(&self) -> Cow<'static, str> {
Expand Down Expand Up @@ -231,7 +230,6 @@ where
T: FromWorld + Send + Sync + 'static,
{
type In = ();

type Out = S::Out;

fn name(&self) -> Cow<'static, str> {
Expand Down
9 changes: 7 additions & 2 deletions release-content/release-notes/observer_overhaul.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Observer Overhaul
authors: ["@Jondolf", "@alice-i-cecile"]
pull_requests: [19596, 19663]
authors: ["@Jondolf", "@alice-i-cecile", "@hukasu]
pull_requests: [19596, 19663, 19611]
---

## Rename `Trigger` to `On`
Expand Down Expand Up @@ -40,3 +40,8 @@ allowing you to bubble events up your hierarchy to see if any of the parents car
then act on the entity that was actually picked in the first place.

This was handy! We've enabled this functionality for all entity-events: simply call `On::original_target`.

## Expose name of the Observer's system

The name of the Observer's system is now accessable through `Observer::system_name`,
this opens up the possibility for the debug tools to show more meaningful names for observers.
Loading