Skip to content

Change event to event_key where it refers to an EventKey #20060

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 4 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/event/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct RegisteredEvent {
event_key: EventKey,
// Required to flush the secondary buffer and drop events even if left unchanged.
previously_updated: bool,
// SAFETY: The component ID and the function must be used to fetch the Events<T> resource
// SAFETY: The `EventKey`'s component ID and the function must be used to fetch the Events<T> resource
// of the same type initialized in `register_event`, or improper type casts will occur.
update: unsafe fn(MutUntyped),
}
Expand Down
30 changes: 15 additions & 15 deletions crates/bevy_ecs/src/observer/distributed_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl Observer {
world.commands().queue(move |world: &mut World| {
let entity = hook_context.entity;
if let Some(mut observe) = world.get_mut::<Observer>(entity) {
if observe.descriptor.events.is_empty() {
if observe.descriptor.event_keys.is_empty() {
return;
}
if observe.error_handler.is_none() {
Expand Down Expand Up @@ -286,13 +286,13 @@ impl Observer {
self
}

/// Observe the given `event`. This will cause the [`Observer`] to run whenever an event with the given [`ComponentId`]
/// Observe the given `event_key`. This will cause the [`Observer`] to run whenever an event with the given [`EventKey`]
/// is triggered.
/// # Safety
/// The type of the `event` [`EventKey`] _must_ match the actual value
/// The type of the `event_key` [`EventKey`] _must_ match the actual value
/// of the event passed into the observer system.
pub unsafe fn with_event(mut self, event: EventKey) -> Self {
self.descriptor.events.push(event);
pub unsafe fn with_event_key(mut self, event_key: EventKey) -> Self {
self.descriptor.event_keys.push(event_key);
self
}

Expand Down Expand Up @@ -349,8 +349,8 @@ impl Component for Observer {
/// This information is stored inside of the [`Observer`] component,
#[derive(Default, Clone)]
pub struct ObserverDescriptor {
/// The events the observer is watching.
pub(super) events: Vec<EventKey>,
/// The event keys the observer is watching.
pub(super) event_keys: Vec<EventKey>,

/// The components the observer is watching.
pub(super) components: Vec<ComponentId>,
Expand All @@ -360,12 +360,12 @@ pub struct ObserverDescriptor {
}

impl ObserverDescriptor {
/// Add the given `events` to the descriptor.
/// Add the given `event_keys` to the descriptor.
/// # Safety
/// The type of each [`EventKey`] in `events` _must_ match the actual value
/// The type of each [`EventKey`] in `event_keys` _must_ match the actual value
/// of the event passed into the observer.
pub unsafe fn with_events(mut self, events: Vec<EventKey>) -> Self {
self.events = events;
pub unsafe fn with_event_keys(mut self, event_keys: Vec<EventKey>) -> Self {
self.event_keys = event_keys;
self
}

Expand All @@ -381,9 +381,9 @@ impl ObserverDescriptor {
self
}

/// Returns the `events` that the observer is watching.
pub fn events(&self) -> &[EventKey] {
&self.events
/// Returns the `event_keys` that the observer is watching.
pub fn event_keys(&self) -> &[EventKey] {
&self.event_keys
}

/// Returns the `components` that the observer is watching.
Expand Down Expand Up @@ -416,7 +416,7 @@ fn hook_on_add<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
components.push(id);
});
if let Some(mut observer) = world.get_mut::<Observer>(entity) {
observer.descriptor.events.push(event_key);
observer.descriptor.event_keys.push(event_key);
observer.descriptor.components.extend(components);

let system: &mut dyn Any = observer.system.as_mut();
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/observer/entity_cloning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn component_clone_observed_by(_source: &SourceComponent, ctx: &mut ComponentClo
.get_mut::<Observer>(observer_entity)
.expect("Source observer entity must have Observer");
observer_state.descriptor.entities.push(target);
let event_keys = observer_state.descriptor.events.clone();
let event_keys = observer_state.descriptor.event_keys.clone();
let components = observer_state.descriptor.components.clone();
for event_key in event_keys {
let observers = world.observers.get_observers_mut(event_key);
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl World {
};
let descriptor = &observer_state.descriptor;

for &event_key in &descriptor.events {
for &event_key in &descriptor.event_keys {
let cache = observers.get_observers_mut(event_key);

if descriptor.components.is_empty() && descriptor.entities.is_empty() {
Expand Down Expand Up @@ -430,7 +430,7 @@ impl World {
let archetypes = &mut self.archetypes;
let observers = &mut self.observers;

for &event_key in &descriptor.events {
for &event_key in &descriptor.event_keys {
let cache = observers.get_observers_mut(event_key);
if descriptor.components.is_empty() && descriptor.entities.is_empty() {
cache.global_observers.remove(&entity);
Expand Down Expand Up @@ -741,7 +741,7 @@ mod tests {
Observer::new(|_: On<Add, A>, mut res: ResMut<Order>| {
res.observed("add/remove");
})
.with_event(on_remove)
.with_event_key(on_remove)
},
);

Expand Down Expand Up @@ -1015,7 +1015,7 @@ mod tests {
Observer::with_dynamic_runner(|mut world, _trigger, _ptr, _propagate| {
world.resource_mut::<Order>().observed("event_a");
})
.with_event(event_a)
.with_event_key(event_a)
};
world.spawn(observe);

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/observer/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'w, E, B: Bundle> DerefMut for On<'w, E, B> {
pub struct ObserverTrigger {
/// The [`Entity`] of the observer handling the trigger.
pub observer: Entity,
/// The [`Event`] the trigger targeted.
/// The [`EventKey`] the trigger targeted.
pub event_key: EventKey,
/// The [`ComponentId`]s the trigger targeted.
pub components: SmallVec<[ComponentId; 2]>,
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_ecs/src/world/deferred_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,18 +781,18 @@ impl<'w> DeferredWorld<'w> {
/// Triggers all event observers for [`ComponentId`] in target.
///
/// # Safety
/// Caller must ensure observers listening for `event` can accept ZST pointers
/// Caller must ensure observers listening for `event_key` can accept ZST pointers
#[inline]
pub(crate) unsafe fn trigger_observers(
&mut self,
event: EventKey,
event_key: EventKey,
target: Option<Entity>,
components: impl Iterator<Item = ComponentId> + Clone,
caller: MaybeLocation,
) {
Observers::invoke::<_>(
self.reborrow(),
event,
event_key,
target,
target,
components,
Expand All @@ -805,11 +805,11 @@ impl<'w> DeferredWorld<'w> {
/// Triggers all event observers for [`ComponentId`] in target.
///
/// # Safety
/// Caller must ensure `E` is accessible as the type represented by `event`
/// Caller must ensure `E` is accessible as the type represented by `event_key`
#[inline]
pub(crate) unsafe fn trigger_observers_with_data<E, T>(
&mut self,
event: EventKey,
event_key: EventKey,
current_target: Option<Entity>,
original_target: Option<Entity>,
components: impl Iterator<Item = ComponentId> + Clone,
Expand All @@ -821,7 +821,7 @@ impl<'w> DeferredWorld<'w> {
{
Observers::invoke::<_>(
self.reborrow(),
event,
event_key,
current_target,
original_target,
components.clone(),
Expand Down Expand Up @@ -849,7 +849,7 @@ impl<'w> DeferredWorld<'w> {
}
Observers::invoke::<_>(
self.reborrow(),
event,
event_key,
Some(current_target),
original_target,
components.clone(),
Expand Down
Loading