bevy/examples/ecs
Joona Aalto e5dc177b4b
Rename Trigger to On (#19596)
# Objective

Currently, the observer API looks like this:

```rust
app.add_observer(|trigger: Trigger<Explode>| {
    info!("Entity {} exploded!", trigger.target());
});
```

Future plans for observers also include "multi-event observers" with a
trigger that looks like this (see [Cart's
example](https://github.com/bevyengine/bevy/issues/14649#issuecomment-2960402508)):

```rust
trigger: Trigger<(
    OnAdd<Pressed>,
    OnRemove<Pressed>,
    OnAdd<InteractionDisabled>,
    OnRemove<InteractionDisabled>,
    OnInsert<Hovered>,
)>,
```

In scenarios like this, there is a lot of repetition of `On`. These are
expected to be very high-traffic APIs especially in UI contexts, so
ergonomics and readability are critical.

By renaming `Trigger` to `On`, we can make these APIs read more cleanly
and get rid of the repetition:

```rust
app.add_observer(|trigger: On<Explode>| {
    info!("Entity {} exploded!", trigger.target());
});
```

```rust
trigger: On<(
    Add<Pressed>,
    Remove<Pressed>,
    Add<InteractionDisabled>,
    Remove<InteractionDisabled>,
    Insert<Hovered>,
)>,
```

Names like `On<Add<Pressed>>` emphasize the actual event listener nature
more than `Trigger<OnAdd<Pressed>>`, and look cleaner. This *also* frees
up the `Trigger` name if we want to use it for the observer event type,
splitting them out from buffered events (bikeshedding this is out of
scope for this PR though).

For prior art:
[`bevy_eventlistener`](https://github.com/aevyrie/bevy_eventlistener)
used
[`On`](https://docs.rs/bevy_eventlistener/latest/bevy_eventlistener/event_listener/struct.On.html)
for its event listener type. Though in our case, the observer is the
event listener, and `On` is just a type containing information about the
triggered event.

## Solution

Steal from `bevy_event_listener` by @aevyrie and use `On`.

- Rename `Trigger` to `On`
- Rename `OnAdd` to `Add`
- Rename `OnInsert` to `Insert`
- Rename `OnReplace` to `Replace`
- Rename `OnRemove` to `Remove`
- Rename `OnDespawn` to `Despawn`

## Discussion

### Naming Conflicts??

Using a name like `Add` might initially feel like a very bad idea, since
it risks conflict with `core::ops::Add`. However, I don't expect this to
be a big problem in practice.

- You rarely need to actually implement the `Add` trait, especially in
modules that would use the Bevy ECS.
- In the rare cases where you *do* get a conflict, it is very easy to
fix by just disambiguating, for example using `ops::Add`.
- The `Add` event is a struct while the `Add` trait is a trait (duh), so
the compiler error should be very obvious.

For the record, renaming `OnAdd` to `Add`, I got exactly *zero* errors
or conflicts within Bevy itself. But this is of course not entirely
representative of actual projects *using* Bevy.

You might then wonder, why not use `Added`? This would conflict with the
`Added` query filter, so it wouldn't work. Additionally, the current
naming convention for observer events does not use past tense.

### Documentation

This does make documentation slightly more awkward when referring to
`On` or its methods. Previous docs often referred to `Trigger::target`
or "sends a `Trigger`" (which is... a bit strange anyway), which would
now be `On::target` and "sends an observer `Event`".

You can see the diff in this PR to see some of the effects. I think it
should be fine though, we may just need to reword more documentation to
read better.
2025-06-12 18:22:33 +00:00
..
change_detection.rs Rename track_change_detection flag to track_location (#17075) 2025-01-01 18:43:47 +00:00
component_hooks.rs Component lifecycle reorganization and documentation (#19543) 2025-06-10 00:59:16 +00:00
custom_query_param.rs Prefer Display over Debug (#16112) 2024-12-27 00:40:06 +00:00
custom_schedule.rs
dynamic.rs Remove deprecated component_reads_and_writes (#16348) 2025-03-04 08:22:29 +00:00
ecs_guide.rs Adopt consistent FooSystems naming convention for system sets (#18900) 2025-05-06 15:18:03 +00:00
entity_disabling.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
error_handling.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
event.rs Rename Timer::finished and Timer::paused to is_finished and is_paused (#19386) 2025-05-27 22:24:18 +00:00
fallible_params.rs Per world error handler (#18810) 2025-05-19 01:35:07 +00:00
fixed_timestep.rs
generic_system.rs Relationships (non-fragmenting, one-to-many) (#17398) 2025-01-18 22:20:30 +00:00
hierarchy.rs Add an example teaching users about custom relationships (#17443) 2025-01-20 23:17:38 +00:00
hotpatching_systems.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
immutable_components.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
iter_combinations.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
nondeterministic_system_order.rs Fix non-functional nondeterministic_system_order example (#10719) 2023-11-25 21:13:35 +00:00
observer_propagation.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
observers.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
one_shot_systems.rs Rename JustifyText to Justify (#19522) 2025-06-09 19:59:48 +00:00
parallel_query.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
relationships.rs Change with_related to work with a Bundle and added with_relationships method (#18699) 2025-04-09 02:34:49 +00:00
removal_detection.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
run_conditions.rs
send_and_receive_events.rs Renamed EventWriter::send methods to write. (#17977) 2025-02-23 21:18:52 +00:00
startup_system.rs
state_scoped.rs Rename Timer::finished and Timer::paused to is_finished and is_paused (#19386) 2025-05-27 22:24:18 +00:00
system_closure.rs Prefer Display over Debug (#16112) 2024-12-27 00:40:06 +00:00
system_param.rs Inverse missing_docs logic (#11676) 2024-02-03 21:40:55 +00:00
system_piping.rs Fix a few typos (#17292) 2025-01-10 22:48:30 +00:00
system_stepping.rs