bevyengine / bevyengine/bevy

Component::register_observers

Open
#23,370 6 comments 0 reactions 0 assignees View on GitHub
A-ECS A-UI C-Feature S-Needs-Goal X-Needs-SME
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

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

A common pattern in Bevy is adding global observers for events that are bubbled through a component. This is used extensively in bevy_ui_widgets, but it's use is not limited to that. Unfortunately, unlike component hooks, which allow automatic registration the first time a component is inserted, with observers you have to add them explicitly, usually via a plugin.

The plugin approach has issues, which are well-discussed elsewhere. Putting that aside for the moment, I think the idea of auto-registration of observers is justified on its own merits.

## What solution would you like?

The `Component` type already has a `register_required_components` method, which is invoked the first time a component is instantiated. I propose adding a sister method, `register_observers`, which can be used to register event listeners for entities with that component. This would be passed an object, similar to `ComponentHooks`, that can only be used to register observers for that specific component - it's not a generic hook that gets passed a `World`.

## What alternative(s) have you considered?

Much ink has been spilled on the idea of "required plugins" as a solution to the problems of complex plugin dependencies, but this is a hard problem to solve. The `register_observers` approach is a relatively straightforward solution.

## Additional context

Note that this would mostly eliminate the need for `UiWidgetsPlugins`. There are a few widgets (such as scrollbars and popover) which register ordinary systems, but most plugins would no longer be needed. It would also mean that users no longer have to pay the cost of adding listeners for widgets they aren't using.

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.