Skip to content

Rename Trigger to On #19596

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

Merged
merged 12 commits into from
Jun 12, 2025
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/observers/propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ fn add_listeners_to_hierarchy<const DENSITY: usize, const N: usize>(
}
}

fn empty_listener<const N: usize>(trigger: Trigger<TestEvent<N>>) {
fn empty_listener<const N: usize>(trigger: On<TestEvent<N>>) {
black_box(trigger);
}
4 changes: 2 additions & 2 deletions benches/benches/bevy_ecs/observers/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::hint::black_box;

use bevy_ecs::{
event::Event,
observer::{Trigger, TriggerTargets},
observer::{On, TriggerTargets},
world::World,
};

Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn observe_simple(criterion: &mut Criterion) {
group.finish();
}

fn empty_listener_base(trigger: Trigger<EventBase>) {
fn empty_listener_base(trigger: On<EventBase>) {
black_box(trigger);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ impl App {

/// Spawns an [`Observer`] entity, which will watch for and respond to the given event.
///
/// `observer` can be any system whose first parameter is a [`Trigger`].
/// `observer` can be any system whose first parameter is [`On`].
///
/// # Examples
///
Expand All @@ -1329,7 +1329,7 @@ impl App {
/// # struct Friend;
/// #
///
/// app.add_observer(|trigger: Trigger<Party>, friends: Query<Entity, With<Friend>>, mut commands: Commands| {
/// app.add_observer(|trigger: On<Party>, friends: Query<Entity, With<Friend>>, mut commands: Commands| {
/// if trigger.event().friends_allowed {
/// for friend in friends.iter() {
/// commands.trigger_targets(Invite, friend);
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_core_widgets/src/core_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_ecs::system::ResMut;
use bevy_ecs::{
component::Component,
entity::Entity,
observer::Trigger,
observer::On,
query::With,
system::{Commands, Query, SystemId},
};
Expand All @@ -28,7 +28,7 @@ pub struct CoreButton {
}

fn button_on_key_event(
mut trigger: Trigger<FocusedInput<KeyboardInput>>,
mut trigger: On<FocusedInput<KeyboardInput>>,
q_state: Query<(&CoreButton, Has<InteractionDisabled>)>,
mut commands: Commands,
) {
Expand All @@ -48,7 +48,7 @@ fn button_on_key_event(
}

fn button_on_pointer_click(
mut trigger: Trigger<Pointer<Click>>,
mut trigger: On<Pointer<Click>>,
mut q_state: Query<(&CoreButton, Has<Pressed>, Has<InteractionDisabled>)>,
mut commands: Commands,
) {
Expand All @@ -63,7 +63,7 @@ fn button_on_pointer_click(
}

fn button_on_pointer_down(
mut trigger: Trigger<Pointer<Press>>,
mut trigger: On<Pointer<Press>>,
mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Pressed>), With<CoreButton>>,
focus: Option<ResMut<InputFocus>>,
focus_visible: Option<ResMut<InputFocusVisible>>,
Expand All @@ -88,7 +88,7 @@ fn button_on_pointer_down(
}

fn button_on_pointer_up(
mut trigger: Trigger<Pointer<Release>>,
mut trigger: On<Pointer<Release>>,
mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Pressed>), With<CoreButton>>,
mut commands: Commands,
) {
Expand All @@ -101,7 +101,7 @@ fn button_on_pointer_up(
}

fn button_on_pointer_drag_end(
mut trigger: Trigger<Pointer<DragEnd>>,
mut trigger: On<Pointer<DragEnd>>,
mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Pressed>), With<CoreButton>>,
mut commands: Commands,
) {
Expand All @@ -114,7 +114,7 @@ fn button_on_pointer_drag_end(
}

fn button_on_pointer_cancel(
mut trigger: Trigger<Pointer<Cancel>>,
mut trigger: On<Pointer<Cancel>>,
mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Pressed>), With<CoreButton>>,
mut commands: Commands,
) {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ struct MyEvent {

let mut world = World::new();

world.add_observer(|trigger: Trigger<MyEvent>| {
world.add_observer(|trigger: On<MyEvent>| {
println!("{}", trigger.event().message);
});

Expand All @@ -339,7 +339,7 @@ struct Explode;
let mut world = World::new();
let entity = world.spawn_empty().id();

world.add_observer(|trigger: Trigger<Explode>, mut commands: Commands| {
world.add_observer(|trigger: On<Explode>, mut commands: Commands| {
println!("Entity {} goes BOOM!", trigger.target().unwrap());
commands.entity(trigger.target().unwrap()).despawn();
});
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,41 +691,41 @@ impl Archetype {
self.flags().contains(ArchetypeFlags::ON_DESPAWN_HOOK)
}

/// Returns true if any of the components in this archetype have at least one [`OnAdd`] observer
/// Returns true if any of the components in this archetype have at least one [`Add`] observer
///
/// [`OnAdd`]: crate::lifecycle::OnAdd
/// [`Add`]: crate::lifecycle::Add
#[inline]
pub fn has_add_observer(&self) -> bool {
self.flags().contains(ArchetypeFlags::ON_ADD_OBSERVER)
}

/// Returns true if any of the components in this archetype have at least one [`OnInsert`] observer
/// Returns true if any of the components in this archetype have at least one [`Insert`] observer
///
/// [`OnInsert`]: crate::lifecycle::OnInsert
/// [`Insert`]: crate::lifecycle::Insert
#[inline]
pub fn has_insert_observer(&self) -> bool {
self.flags().contains(ArchetypeFlags::ON_INSERT_OBSERVER)
}

/// Returns true if any of the components in this archetype have at least one [`OnReplace`] observer
/// Returns true if any of the components in this archetype have at least one [`Replace`] observer
///
/// [`OnReplace`]: crate::lifecycle::OnReplace
/// [`Replace`]: crate::lifecycle::Replace
#[inline]
pub fn has_replace_observer(&self) -> bool {
self.flags().contains(ArchetypeFlags::ON_REPLACE_OBSERVER)
}

/// Returns true if any of the components in this archetype have at least one [`OnRemove`] observer
/// Returns true if any of the components in this archetype have at least one [`Remove`] observer
///
/// [`OnRemove`]: crate::lifecycle::OnRemove
/// [`Remove`]: crate::lifecycle::Remove
#[inline]
pub fn has_remove_observer(&self) -> bool {
self.flags().contains(ArchetypeFlags::ON_REMOVE_OBSERVER)
}

/// Returns true if any of the components in this archetype have at least one [`OnDespawn`] observer
/// Returns true if any of the components in this archetype have at least one [`Despawn`] observer
///
/// [`OnDespawn`]: crate::lifecycle::OnDespawn
/// [`Despawn`]: crate::lifecycle::Despawn
#[inline]
pub fn has_despawn_observer(&self) -> bool {
self.flags().contains(ArchetypeFlags::ON_DESPAWN_OBSERVER)
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use crate::{
RequiredComponents, StorageType, Tick,
},
entity::{Entities, Entity, EntityLocation},
lifecycle::{ON_ADD, ON_INSERT, ON_REMOVE, ON_REPLACE},
lifecycle::{ADD, INSERT, REMOVE, REPLACE},
observer::Observers,
prelude::World,
query::DebugCheckedUnwrap,
Expand Down Expand Up @@ -1191,7 +1191,7 @@ impl<'w> BundleInserter<'w> {
if insert_mode == InsertMode::Replace {
if archetype.has_replace_observer() {
deferred_world.trigger_observers(
ON_REPLACE,
REPLACE,
Some(entity),
archetype_after_insert.iter_existing(),
caller,
Expand Down Expand Up @@ -1376,7 +1376,7 @@ impl<'w> BundleInserter<'w> {
);
if new_archetype.has_add_observer() {
deferred_world.trigger_observers(
ON_ADD,
ADD,
Some(entity),
archetype_after_insert.iter_added(),
caller,
Expand All @@ -1394,7 +1394,7 @@ impl<'w> BundleInserter<'w> {
);
if new_archetype.has_insert_observer() {
deferred_world.trigger_observers(
ON_INSERT,
INSERT,
Some(entity),
archetype_after_insert.iter_inserted(),
caller,
Expand All @@ -1413,7 +1413,7 @@ impl<'w> BundleInserter<'w> {
);
if new_archetype.has_insert_observer() {
deferred_world.trigger_observers(
ON_INSERT,
INSERT,
Some(entity),
archetype_after_insert.iter_added(),
caller,
Expand Down Expand Up @@ -1567,7 +1567,7 @@ impl<'w> BundleRemover<'w> {
};
if self.old_archetype.as_ref().has_replace_observer() {
deferred_world.trigger_observers(
ON_REPLACE,
REPLACE,
Some(entity),
bundle_components_in_archetype(),
caller,
Expand All @@ -1582,7 +1582,7 @@ impl<'w> BundleRemover<'w> {
);
if self.old_archetype.as_ref().has_remove_observer() {
deferred_world.trigger_observers(
ON_REMOVE,
REMOVE,
Some(entity),
bundle_components_in_archetype(),
caller,
Expand Down Expand Up @@ -1833,7 +1833,7 @@ impl<'w> BundleSpawner<'w> {
);
if archetype.has_add_observer() {
deferred_world.trigger_observers(
ON_ADD,
ADD,
Some(entity),
bundle_info.iter_contributed_components(),
caller,
Expand All @@ -1848,7 +1848,7 @@ impl<'w> BundleSpawner<'w> {
);
if archetype.has_insert_observer() {
deferred_world.trigger_observers(
ON_INSERT,
INSERT,
Some(entity),
bundle_info.iter_contributed_components(),
caller,
Expand Down Expand Up @@ -2386,7 +2386,7 @@ mod tests {
#[derive(Resource, Default)]
struct Count(u32);
world.init_resource::<Count>();
world.add_observer(|_t: Trigger<ArchetypeCreated>, mut count: ResMut<Count>| {
world.add_observer(|_t: On<ArchetypeCreated>, mut count: ResMut<Count>| {
count.0 += 1;
});

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ mod private {
/// `&mut ...`, created while inserted onto an entity.
/// In all other ways, they are identical to mutable components.
/// This restriction allows hooks to observe all changes made to an immutable
/// component, effectively turning the `OnInsert` and `OnReplace` hooks into a
/// component, effectively turning the `Insert` and `Replace` hooks into a
/// `OnMutate` hook.
/// This is not practical for mutable components, as the runtime cost of invoking
/// a hook for every exclusive reference created would be far too high.
Expand Down Expand Up @@ -2415,7 +2415,7 @@ impl Tick {
/// struct CustomSchedule(Schedule);
///
/// # let mut world = World::new();
/// world.add_observer(|tick: Trigger<CheckChangeTicks>, mut schedule: ResMut<CustomSchedule>| {
/// world.add_observer(|tick: On<CheckChangeTicks>, mut schedule: ResMut<CustomSchedule>| {
/// schedule.0.check_change_ticks(tick.get());
/// });
/// ```
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_ecs/src/event/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ use core::{
note = "consider annotating `{Self}` with `#[derive(Event)]`"
)]
pub trait Event: Send + Sync + 'static {
/// The component that describes which Entity to propagate this event to next, when [propagation] is enabled.
/// The component that describes which [`Entity`] to propagate this event to next, when [propagation] is enabled.
///
/// [propagation]: crate::observer::Trigger::propagate
/// [`Entity`]: crate::entity::Entity
/// [propagation]: crate::observer::On::propagate
type Traversal: Traversal<Self>;

/// When true, this event will always attempt to propagate when [triggered], without requiring a call
/// to [`Trigger::propagate`].
/// to [`On::propagate`].
///
/// [triggered]: crate::system::Commands::trigger_targets
/// [`Trigger::propagate`]: crate::observer::Trigger::propagate
/// [`On::propagate`]: crate::observer::On::propagate
const AUTO_PROPAGATE: bool = false;

/// Generates the [`ComponentId`] for this event type.
Expand Down
11 changes: 9 additions & 2 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ use event::Event;
/// This includes the most common types in this crate, re-exported for your convenience.
pub mod prelude {
#[doc(hidden)]
#[expect(
deprecated,
reason = "`Trigger` was deprecated in favor of `On`, and `OnX` lifecycle events were deprecated in favor of `X` events."
)]
pub use crate::{
bundle::Bundle,
change_detection::{DetectChanges, DetectChangesMut, Mut, Ref},
Expand All @@ -76,9 +80,12 @@ pub mod prelude {
error::{BevyError, Result},
event::{Event, EventMutator, EventReader, EventWriter, Events},
hierarchy::{ChildOf, ChildSpawner, ChildSpawnerCommands, Children},
lifecycle::{OnAdd, OnDespawn, OnInsert, OnRemove, OnReplace, RemovedComponents},
lifecycle::{
Add, Despawn, Insert, OnAdd, OnDespawn, OnInsert, OnRemove, OnReplace, Remove,
RemovedComponents, Replace,
},
name::{Name, NameOrEntity},
observer::{Observer, Trigger},
observer::{Observer, On, Trigger},
query::{Added, Allows, AnyOf, Changed, Has, Or, QueryBuilder, QueryState, With, Without},
related,
relationship::RelationshipTarget,
Expand Down
Loading
Loading