Skip to content

Migrate Observers to use Relationships #17607

Open
@viridia

Description

@viridia

What problem does this solve or what need does it fill?

Currently the .observe() method on an entity creates a hidden satellite entity to hold the observer. This satellite entity is linked to the parent via a bespoke mechanism, so that the observer is despawned when the parent is. This mechanism should instead be replaced by the new Relationship mechanism recently introduced.

This has a number of advantages, the main one being that it allows observers to be defined declaratively, using a syntax that is consistent with the way child entities are defined.

When combined with the new spawning PR, this would permit syntax like the following:

commands.spawn((
    Node::default(),
    children![
        // Children go here
    ],
    observers![
        Observer::new(|trigger: Trigger<Pointer<Click>>, mut commands: Commands| {
            // etc.
        }),
        Observer::new(|trigger: Trigger<Pointer<Down>>, mut commands: Commands| {
            // etc.
        }),
    ]
)

What solution would you like?

We would define a new Relationship, Observers and it's counterpart ObserverOf (or Observing if you want to get cute). This is essentially a cut & paste of Children with the names changed.

The existing .observe() method would be changed to insert an ObserverOf component, for backwards compatibility.

What alternative(s) have you considered?

Technically, a third-party crate could define it's own Observers relationship and things would work. However, it would be best if this was centralized within Bevy so that multiple authors didn't end up defining their own observer solutions.

Additional context

Backwards compatibility considerations: Ideally, there should be little or no migration required for "ordinary" users of observers. However, there are two classes of advanced users which might be affected:

  • Inspectors that display a hierarchy of children and filter out observers (currently observers appear at the root level since they have no parents)
  • Code that iterates through the components of an entity (since now there will be an Observers component)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleD-ComplexQuite challenging from either a design or technical perspective. Ask for help!M-Needs-Migration-GuideA breaking change to Bevy's public API that needs to be noted in a migration guideS-Needs-DesignThis issue requires design work to think about how it would best be accomplishedX-BlessedHas a large architectural impact or tradeoffs, but the design has been endorsed by decision makers

    Type

    No type

    Projects

    Status

    Observer overhaul

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions