bevyengine / bevyengine/bevy

Migrate Observers to use Relationships

Open
#17,607 12 comments 30 reactions 1 assignee Claimed by @alice-i-cecile View on GitHub
A-ECS C-Feature D-Complex M-Migration-Guide S-Blocked X-Blessed
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## 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:

```rust
commands.spawn((
Node::default(),
children![
// Children go here
],
observers![
Observer::new(|trigger: Trigger>, mut commands: Commands| {
// etc.
}),
Observer::new(|trigger: Trigger>, 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)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.