Skip to content

Commit 01594ad

Browse files
committed
Fill in and update remaining docs
1 parent a5c318b commit 01594ad

File tree

17 files changed

+140
-53
lines changed

17 files changed

+140
-53
lines changed

crates/bevy_animation/src/animation_event.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ use bevy_ecs::{
77
world::DeferredWorld,
88
};
99

10+
/// An [`Event`] that an [`AnimationPlayer`](crate::AnimationPlayer) can trigger when playing an [`AnimationClip`](crate::AnimationClip).
11+
/// See [`AnimationClip::add_event`](crate::AnimationClip::add_event).
12+
///
13+
/// This trait can be derived.
1014
pub trait AnimationEvent: Clone + for<'a> Event<Trigger<'a> = AnimationEventTrigger> {}
1115

16+
/// The [`Trigger`] implemention for [`AnimationEvent`]. This passes in the [`AnimationPlayer`](crate::AnimationPlayer)
17+
/// context, and uses that to run any observers that target that entity.
1218
pub struct AnimationEventTrigger {
19+
/// The [`AnimationPlayer`](crate::AnimationPlayer) where this [`AnimationEvent`] occurred.
1320
pub animation_player: Entity,
1421
}
1522

16-
impl<E: Event> Trigger<E> for AnimationEventTrigger {
23+
impl<E: AnimationEvent> Trigger<E> for AnimationEventTrigger {
1724
fn trigger(
1825
&mut self,
1926
world: DeferredWorld,

crates/bevy_core_widgets/src/core_checkbox.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub struct SetChecked {
127127
/// ```
128128
#[derive(EntityEvent)]
129129
pub struct ToggleChecked {
130+
/// The [`Entity`] of the toggled [`CoreCheckbox`]
130131
pub entity: Entity,
131132
}
132133

crates/bevy_core_widgets/src/core_slider.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ pub struct SetSliderValue {
519519
pub change: SliderValueChange,
520520
}
521521

522+
/// The type of slider value change to apply in [`SetSliderValue`].
522523
#[derive(Clone)]
523524
pub enum SliderValueChange {
524525
/// Set the slider value to a specific value.

crates/bevy_ecs/macros/src/component.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn derive_entity_event(input: TokenStream) -> TokenStream {
8686
processed_attrs.push(AUTO_PROPAGATE);
8787
Ok(())
8888
}
89+
#[allow(deprecated)]
8990
Some(ident) if ident == TRAVERSAL => {
9091
Err(meta.error(
9192
"`traversal` has been renamed to `propagate`, use that instead. If you were writing `traversal = &'static ChildOf`, you can now just write `propagate`, which defaults to the `ChildOf` traversal."

crates/bevy_ecs/src/event/buffered_event/registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ struct RegisteredEvent {
1717
update: unsafe fn(MutUntyped),
1818
}
1919

20-
/// A registry of all of the [`Events`] in the [`World`], used by [`event_update_system`](crate::event::update::event_update_system)
20+
/// A registry of all of the [`Events`] in the [`World`], used by [`event_update_system`](crate::event::event_update_system)
2121
/// to update all of the events.
2222
#[derive(Resource, Default)]
2323
pub struct EventRegistry {
2424
/// Should the events be updated?
2525
///
26-
/// This field is generally automatically updated by the [`signal_event_update_system`](crate::event::update::signal_event_update_system).
26+
/// This field is generally automatically updated by the [`signal_event_update_system`](crate::event::signal_event_update_system).
2727
pub should_update: ShouldUpdateEvents,
2828
event_updates: Vec<RegisteredEvent>,
2929
}

crates/bevy_ecs/src/event/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ pub trait Event: Send + Sync + Sized + 'static {
229229
/// entity: Entity,
230230
/// }
231231
/// ```
232+
233+
/// You can also _stop_ propagation like this:
234+
/// ```
235+
/// # use bevy_ecs::prelude::*;
236+
/// # #[derive(EntityEvent)]
237+
/// # #[entity_event(propagate)]
238+
/// # struct Click {
239+
/// # entity: Entity,
240+
/// # }
241+
/// # fn is_finished_propagating() -> bool { true }
242+
/// # let mut world = World::default();
243+
/// world.add_observer(|mut click: On<Click>| {
244+
/// if is_finished_propagating() {
245+
/// click.propagate(false);
246+
/// }
247+
/// });
248+
/// ```
232249
///
233250
/// ## Naming and Usage Conventions
234251
///
@@ -316,7 +333,7 @@ struct EventWrapperComponent<E: Event>(PhantomData<E>);
316333

317334
/// A unique identifier for an [`Event`], used by [observers].
318335
///
319-
/// You can look up the key for your event by calling the [`Event::event_key`] method.
336+
/// You can look up the key for your event by calling the [`World::event_key`] method.
320337
///
321338
/// [observers]: crate::observer
322339
#[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]

crates/bevy_ecs/src/event/trigger.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@ use crate::{
99
use bevy_ptr::PtrMut;
1010
use core::marker::PhantomData;
1111

12+
/// [`Trigger`] determines _how_ an [`Event`] is triggered when [`World::trigger`](crate::world::World::trigger) is called.
13+
/// This decides which [`Observer`](crate::observer::Observer)s will run, what data gets passed to them, and the order they will
14+
/// be executed in.
15+
///
16+
/// Implementing [`Trigger`] is "advanced-level" terrority, and is generally unnecessary unless you are developing highly specialized
17+
/// [`Event`] trigger logic.
18+
///
19+
/// Bevy comes with a number of built-in [`Trigger`] implementations (see their documentation for more info):
20+
/// - [`GlobalTrigger`]: The [`Event`] derive defaults to using this
21+
/// - [`EntityTrigger`]: The [`EntityEvent`](crate::event::EntityEvent) derive defaults to using this
22+
/// - [`PropagateEntityTrigger`]: The [`EntityEvent`](crate::event::EntityEvent) derive uses this when propagation is enabled.
23+
/// - [`EntityComponentsTrigger`]: Used by Bevy's [component lifecycle events](crate::lifecycle).
1224
pub trait Trigger<E: Event> {
25+
/// Trigger the given `event`, running every [`Observer`](crate::observer::Observer) that matches the `event`, as defined by this
26+
/// [`Trigger`] and the state stored on `self`.
1327
fn trigger(
1428
&mut self,
1529
world: DeferredWorld,
@@ -19,6 +33,10 @@ pub trait Trigger<E: Event> {
1933
);
2034
}
2135

36+
/// A [`Trigger`] that runs _every_ "global" [`Observer`](crate::observer::Observer) (ex: registered via [`World::add_observer`](crate::world::World::add_observer))
37+
/// that matches the given [`Event`].
38+
///
39+
/// The [`Event`] derive defaults to using this [`Trigger`], and it is usable for any [`Event`] type.
2240
#[derive(Default)]
2341
pub struct GlobalTrigger;
2442

@@ -58,6 +76,14 @@ impl GlobalTrigger {
5876
}
5977
}
6078

79+
/// An [`EntityEvent`] [`Trigger`] that does two things:
80+
/// - Runs all "global" [`Observer`] (ex: registered via [`World::add_observer`](crate::world::World::add_observer))
81+
/// that matches the given [`Event`]. This is the same behavior as [`GlobalTrigger`].
82+
/// - Runs every "entity scoped" [`Observer`] that watches the given [`EntityEvent::event_target`] entity.
83+
///
84+
/// The [`EntityEvent`] derive defaults to using this [`Trigger`], and it is usable for any [`EntityEvent`] type.
85+
///
86+
/// [`Observer`]: crate::observer::Observer
6187
#[derive(Default)]
6288
pub struct EntityTrigger;
6389

@@ -81,8 +107,8 @@ impl<E: EntityEvent> Trigger<E> for EntityTrigger {
81107
}
82108
}
83109

84-
/// Trigger observers listening for the given entity event.
85-
/// The `target_entity` should match the `EntityEvent::entity` on `event` for logical correctness.
110+
/// Trigger observers watching for the given entity event.
111+
/// The `target_entity` should match the [`EntityEvent::event_target`] on `event` for logical correctness.
86112
// Note: this is not an EntityTrigger method because we want to reuse this logic for the entity propagation trigger
87113
#[inline(never)]
88114
pub fn trigger_entity_internal(
@@ -120,9 +146,22 @@ pub fn trigger_entity_internal(
120146
}
121147
}
122148

149+
/// An [`EntityEvent`] [`Trigger`] that behaves like [`EntityTrigger`], but "propagates" the event
150+
/// using an [`Entity`] [`Traversal`]. At each step in the propagation, the [`EntityTrigger`] logic will
151+
/// be run, until [`PropagateEntityTrigger::propagate`] is false, or there are no entities left to traverse.
152+
///
153+
/// This is used by the [`EntityEvent`] derive when `#[entity_event(propagate)]` is enabled. It is usable by every
154+
/// [`EntityEvent`] type.
155+
///
156+
/// If `AUTO_PROPAGATE` is `true`, [`PropagateEntityTrigger::propagate`] will default to `true`.
123157
pub struct PropagateEntityTrigger<const AUTO_PROPAGATE: bool, E: EntityEvent, T: Traversal<E>> {
158+
/// The original [`Entity`] the [`Event`] was _first_ triggered for.
124159
pub original_event_target: Entity,
160+
161+
/// Whether or not to continue propagating using the `T` [`Traversal`]. If this is false,
162+
/// The [`Traversal`] will stop on the current entity.
125163
pub propagate: bool,
164+
126165
_marker: PhantomData<(E, T)>,
127166
}
128167

@@ -185,6 +224,11 @@ impl<const AUTO_PROPAGATE: bool, E: EntityEvent, T: Traversal<E>> Trigger<E>
185224
}
186225
}
187226

227+
/// An [`EntityEvent`] [`Trigger`] that, in addition to behaving like a normal [`EntityTrigger`], _also_ runs observers
228+
/// that watch for components that match the slice of [`ComponentId`]s referenced in [`EntityComponentsTrigger`]. This includes
229+
/// both _global_ observers of those components and "entity scoped" observers that watch the [`EntityEvent::event_target`].
230+
///
231+
/// This is used by Bevy's built-in [lifecycle events](crate::lifecycle).
188232
#[derive(Default)]
189233
pub struct EntityComponentsTrigger<'a>(pub &'a [ComponentId]);
190234

@@ -220,7 +264,7 @@ impl<'a> EntityComponentsTrigger<'a> {
220264
trigger_context,
221265
);
222266

223-
// Trigger observers listening to this trigger targeting a specific component
267+
// Trigger observers watching for a specific component
224268
for id in self.0 {
225269
if let Some(component_observers) = observers.component_observers().get(id) {
226270
for (observer, runner) in component_observers.global_observers() {

crates/bevy_ecs/src/lifecycle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
//!
4444
//! Despite the absence of generics, each lifecycle event is associated with a specific component.
4545
//! When defining a component hook for a [`Component`] type, that component is used.
46-
//! When listening to lifecycle events for observers, the `B: Bundle` generic is used.
46+
//! When observers watch lifecycle events, the `B: Bundle` generic is used.
4747
//!
4848
//! Each of these lifecycle events also corresponds to a fixed [`ComponentId`],
4949
//! which are assigned during [`World`] initialization.

crates/bevy_ecs/src/observer/centralized_storage.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ use crate::{
1818

1919
/// An internal lookup table tracking all of the observers in the world.
2020
///
21-
/// Stores a cache mapping trigger ids to the registered observers.
21+
/// Stores a cache mapping event ids to their registered observers.
2222
/// Some observer kinds (like [lifecycle](crate::lifecycle) observers) have a dedicated field,
2323
/// saving lookups for the most common triggers.
2424
///
2525
/// This can be accessed via [`World::observers`].
2626
#[derive(Default, Debug)]
2727
pub struct Observers {
28-
// Cached ECS observers to save a lookup most common triggers.
28+
// Cached ECS observers to save a lookup for high-traffic built-in event types.
2929
add: CachedObservers,
3030
insert: CachedObservers,
3131
replace: CachedObservers,
3232
remove: CachedObservers,
3333
despawn: CachedObservers,
34-
// Map from trigger type to set of observers listening to that trigger
34+
// Map from event type to set of observers watching for that event
3535
cache: HashMap<EventKey, CachedObservers>,
3636
}
3737

@@ -111,28 +111,28 @@ impl Observers {
111111
/// This is stored inside of [`Observers`], specialized for each kind of observer.
112112
#[derive(Default, Debug)]
113113
pub struct CachedObservers {
114-
// Observers listening for any time this event is fired, regardless of target
115-
// This will also respond to events targeting specific components or entities
114+
/// Observers watching for any time this event is triggered, regardless of target.
115+
/// These will also respond to events targeting specific components or entities
116116
pub(super) global_observers: ObserverMap,
117-
// Observers listening for this trigger fired at a specific component
117+
/// Observers watching for triggers of events for a specific component
118118
pub(super) component_observers: HashMap<ComponentId, CachedComponentObservers>,
119-
// Observers listening for this trigger fired at a specific entity
119+
/// Observers watching for triggers of events for a specific entity
120120
pub(super) entity_observers: EntityHashMap<ObserverMap>,
121121
}
122122

123123
impl CachedObservers {
124-
/// Returns the observers listening for this trigger, regardless of target.
125-
/// These observers will also respond to events targeting specific components or entities.
124+
/// Observers watching for any time this event is triggered, regardless of target.
125+
/// These will also respond to events targeting specific components or entities
126126
pub fn global_observers(&self) -> &ObserverMap {
127127
&self.global_observers
128128
}
129129

130-
/// Returns the observers listening for this trigger targeting components.
130+
/// Returns observers watching for triggers of events for a specific component.
131131
pub fn component_observers(&self) -> &HashMap<ComponentId, CachedComponentObservers> {
132132
&self.component_observers
133133
}
134134

135-
/// Returns the observers listening for this trigger targeting entities.
135+
/// Returns observers watching for triggers of events for a specific entity.
136136
pub fn entity_observers(&self) -> &EntityHashMap<ObserverMap> {
137137
&self.entity_observers
138138
}
@@ -146,20 +146,19 @@ pub type ObserverMap = EntityHashMap<ObserverRunner>;
146146
/// This is stored inside of [`CachedObservers`].
147147
#[derive(Default, Debug)]
148148
pub struct CachedComponentObservers {
149-
// Observers listening to events targeting this component, but not a specific entity
149+
// Observers watching for events targeting this component, but not a specific entity
150150
pub(super) global_observers: ObserverMap,
151-
// Observers listening to events targeting this component on a specific entity
151+
// Observers wathing for events targeting this component on a specific entity
152152
pub(super) entity_component_observers: EntityHashMap<ObserverMap>,
153153
}
154154

155155
impl CachedComponentObservers {
156-
/// Returns the observers listening for this trigger, regardless of target.
157-
/// These observers will also respond to events targeting specific entities.
156+
/// Returns observers watching for events targeting this component, but not a specific entity
158157
pub fn global_observers(&self) -> &ObserverMap {
159158
&self.global_observers
160159
}
161160

162-
/// Returns the observers listening for this trigger targeting this component on a specific entity.
161+
/// Returns observers wathing for events targeting this component on a specific entity
163162
pub fn entity_component_observers(&self) -> &EntityHashMap<ObserverMap> {
164163
&self.entity_component_observers
165164
}

crates/bevy_ecs/src/observer/distributed_storage.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use crate::prelude::ReflectComponent;
7272
/// world.spawn(Observer::new(|event: On<Speak>| {}));
7373
/// ```
7474
///
75-
/// Observers are a specialized [`System`] called called an [`ObserverSystem`]. The first parameter must be [`On`], which provides access
75+
/// Observers are a specialized [`System`] called an [`ObserverSystem`]. The first parameter must be [`On`], which provides access
7676
/// to the [`Event`], the [`Trigger`], and some additional execution context.
7777
///
7878
/// Because they are systems, they can access arbitrary [`World`] data by adding [`SystemParam`]s:
@@ -123,7 +123,7 @@ use crate::prelude::ReflectComponent;
123123
/// recursively evaluated until there are no commands left, meaning nested triggers all
124124
/// evaluate at the same time!
125125
///
126-
/// ## Event [`Trigger`] Behavior
126+
/// ## Event [`Trigger`] behavior
127127
///
128128
/// Each [`Event`] defines a [`Trigger`] behavior, which determines _which_ observers will run for the given [`Event`] and _how_ they will be run.
129129
///
@@ -140,7 +140,7 @@ use crate::prelude::ReflectComponent;
140140
///
141141
/// You can also define your own!
142142
///
143-
/// ## Observer Execution Timing
143+
/// ## Observer execution timing
144144
///
145145
/// Observers triggered via [`World::trigger`] are evaluated immediately, as are all commands they queue up.
146146
///
@@ -174,10 +174,10 @@ use crate::prelude::ReflectComponent;
174174
/// If an [`EntityEvent`] [`Observer`] targets specific entities, and all of those entities are despawned, the [`Observer`] entity will also be despawned.
175175
/// This protects against observer "garbage" building up over time.
176176
///
177-
/// ## Component Lifecycle events: Observers vs Hooks
177+
/// ## Component lifecycle events: Observers vs Hooks
178178
///
179179
/// It is important to note that observers, just like [hooks](crate::lifecycle::ComponentHooks),
180-
/// can listen to and respond to [lifecycle](crate::lifecycle) events.
180+
/// can watch for and respond to [lifecycle](crate::lifecycle) events.
181181
/// Unlike hooks, observers are not treated as an "innate" part of component behavior:
182182
/// they can be added or removed at runtime, and multiple observers
183183
/// can be registered for the same lifecycle event for the same component.
@@ -190,12 +190,12 @@ use crate::prelude::ReflectComponent;
190190
/// This allows hooks to act as constructors and destructors for components,
191191
/// as they always have the first and final say in the component's lifecycle.
192192
///
193-
/// ## Observer Re-targeting
193+
/// ## Observer re-targeting
194194
///
195195
/// Currently, [observers cannot be retargeted after spawning](https://github.com/bevyengine/bevy/issues/19587):
196196
/// despawn and respawn an observer as a workaround.
197197
///
198-
/// ## Internal observer Cache
198+
/// ## Internal observer cache
199199
///
200200
/// For more efficient observer triggering, Observers make use of the internal [`CachedObservers`](crate::observer::CachedObservers) storage.
201201
/// In general, this is an implementation detail developers don't need to worry about, but it can be used when implementing custom [`Trigger`](crate::event::Trigger)

0 commit comments

Comments
 (0)