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 5 commits into
base: main
Choose a base branch
from

Conversation

hukasu
Copy link
Contributor

@hukasu hukasu commented Jun 13, 2025

Objective

Fixes #18726
Alternative to and closes #18797

Solution

Create a method Observer::system_name to expose the name of the Observer's system

Showcase

// Returns `my_crate::my_observer`
let observer = Observer::new(my_observer);
println!(observer.system_name());

// Returns `my_crate::method::{{closure}}`
let observer = Observer::new(|_trigger: Trigger<...>|);
println!(observer.system_name());

// Returns `custom_name`
let observer = Observer::new(IntoSystem::into_system(my_observer).with_name("custom_name"));
println!(observer.system_name());

TODO

  • Achieve cart's approval

@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Jun 13, 2025
@hukasu
Copy link
Contributor Author

hukasu commented Jun 13, 2025

aylmao, the test failed because it uses Trigger<OnAdd, Name> for the test observers, and now i'm add Name for every observer...

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Release-Note Work that should be called out in the blog due to impact X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Jun 13, 2025
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, this is really clean. Two small things to clean up:

  1. We should probably do something like "type_name Observer" for the name. Make a method on Observer or a free function to define the exact standard name, then call that in the couple of places where we're defining the name.
  2. We should lump these changes in with the existing observer_overhaul release notes. Don't worry too much about making it nice; just move the content you have over and add yourself + this PR to the authors list. We'll need a big rewrite at the end of it to cover all of these changes.

@alice-i-cecile alice-i-cecile added the S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged label Jun 13, 2025
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a suggestion to avoid a new public Bundle type. Once that's fixed, this LGTM :) Thanks!

@mgi388
Copy link
Contributor

mgi388 commented Jun 13, 2025

@alice-i-cecile do you think it would be a reasonable follow up to this PR (but not in it) to make this behind a feature (dunno which, debug or entity_names) so that the observer Name can be excluded in release builds (and I’m making the assumption that it’s sole purpose is for debugging or visualization rather than powering actual observer functionality)?

I definitely don’t want to be prematurely optimizing but also I’ve seen the massive list of observers in inspector egui at least in my game and I’d be surprised if I want all those entities in my release build hanging around with Names. Happy to take this conversation elsewhere but this PR prompted the thought.

@hukasu
Copy link
Contributor Author

hukasu commented Jun 13, 2025

@alice-i-cecile they need to be at least pub(crate) to be visible by the World, Commands and EntityWorldMut

@hukasu hukasu changed the title Alternative Named Observer Named Observer (attempt 2) Jun 13, 2025
@alice-i-cecile
Copy link
Member

@alice-i-cecile do you think it would be a reasonable follow up to this PR (but not in it) to make this behind a feature (dunno which, debug or entity_names) so that the observer Name can be excluded in release builds (and I’m making the assumption that it’s sole purpose is for debugging or visualization rather than powering actual observer functionality)?

I definitely don’t want to be prematurely optimizing but also I’ve seen the massive list of observers in inspector egui at least in my game and I’d be surprised if I want all those entities in my release build hanging around with Names. Happy to take this conversation elsewhere but this PR prompted the thought.

This should be done as part of #19558, or in follow-up after both these PRs are merged. Good thought!

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my request wasn't clear. I just wanted to use the anonymous tuple type, not to make the constructor private :) I've left a final suggestion with my intended fix, then this LGTM.

@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jun 13, 2025
@Atlas16A Atlas16A added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 13, 2025
Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool!

Should there be any unit tests for this, where we spawn an observer and check that is has a Name component with a useful value? Or maybe they would be too brittle because type_name doesn't have any stability guarantees.

@Atlas16A Atlas16A added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jun 13, 2025
@alice-i-cecile alice-i-cecile removed the X-Uncontroversial This work is generally agreed upon label Jun 13, 2025
@alice-i-cecile alice-i-cecile added the X-Contentious There are nontrivial implications that should be thought through label Jun 13, 2025
@hukasu
Copy link
Contributor Author

hukasu commented Jun 15, 2025

the rebase wrecked everything... will restart

@hukasu hukasu force-pushed the named-observer-2 branch from e77ef26 to 830199f Compare June 15, 2025 13:51
@hukasu hukasu force-pushed the named-observer-2 branch from 830199f to 98d5a47 Compare June 16, 2025 00:55
@hukasu hukasu changed the title Named Observer (attempt 2) Exposes Observer's system's name Jun 16, 2025
@hukasu
Copy link
Contributor Author

hukasu commented Jun 16, 2025

i don't know if this new attempt still count as solving the issue raised by #18726

@hukasu hukasu added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jun 16, 2025
@alice-i-cecile
Copy link
Member

i don't know if this new attempt still count as solving the issue raised by #18726

This will be good enough: inspectors can manually call these methods to generate names for now. We can add Name components later behind a feature flag.

@chescock
Copy link
Contributor

Splitting up the System trait like that is a lot of churn, and it doesn't seem like other code benefits from the split. Would it make sense to localize the change by making NamedSystem use a blanket impl instead of being a base trait? You could even merge it with AnyNamedSystem at that point. That is, something like:

trait AnyNamedSystem: Any + Send + Sync + 'static {
    fn system_name(&self) -> Cow<'static, str>;
}
impl<S: System> AnyNamedSystem for S {
    fn system_name(&self) -> Cow<'static, str> { self.name() }
}
// ...
pub struct Observer {
    // ...
    system: Box<dyn AnyNamedSystem>,

@@ -49,8 +49,10 @@ pub trait System: Send + Sync + 'static {
type In: SystemInput;
/// The system's output.
type Out;
/// Returns the system's name.

/// Returns system's name.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this still have "the", for grammar's sake?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add named observers
7 participants