@@ -200,8 +200,7 @@ use crate::prelude::ReflectComponent;
200
200
///
201
201
/// Note that the [`Observer`] component is not added to the entity it is observing. Observers should always be their own entities!
202
202
///
203
- /// You can call [`Observer::watch_entity`] more than once, which allows you to watch multiple entities with the same [`Observer`].
204
- /// serves as the "source of truth" of the observer.
203
+ /// You can call [`Observer::watch_entity`] more than once or [`Observer::watch_entities`] to watch multiple entities with the same [`Observer`].
205
204
///
206
205
/// [`SystemParam`]: crate::system::SystemParam
207
206
pub struct Observer {
@@ -269,28 +268,44 @@ impl Observer {
269
268
}
270
269
}
271
270
272
- /// Observe the given `entity`. This will cause the [`Observer`] to run whenever the [`Event`] is triggered
273
- /// for the `entity`.
271
+ /// Observes the given `entity` (in addition to any entity already being observed).
272
+ /// This will cause the [`Observer`] to run whenever the [`Event`] is triggered for the `entity`.
273
+ /// Note that if this is called _after_ an [`Observer`] is spawned, it will produce no effects.
274
274
pub fn with_entity ( mut self , entity : Entity ) -> Self {
275
- self . descriptor . entities . push ( entity) ;
275
+ self . watch_entity ( entity) ;
276
276
self
277
277
}
278
278
279
- /// Observe the given `entity`. This will cause the [`Observer`] to run whenever the [`Event`] is triggered
280
- /// for the `entity`.
279
+ /// Observes the given `entities` (in addition to any entity already being observed).
280
+ /// This will cause the [`Observer`] to run whenever the [`Event`] is triggered for any of these `entities`.
281
+ /// Note that if this is called _after_ an [`Observer`] is spawned, it will produce no effects.
282
+ pub fn with_entities < I : IntoIterator < Item = Entity > > ( mut self , entities : I ) -> Self {
283
+ self . watch_entities ( entities) ;
284
+ self
285
+ }
286
+
287
+ /// Observes the given `entity` (in addition to any entity already being observed).
288
+ /// This will cause the [`Observer`] to run whenever the [`Event`] is triggered for the `entity`.
281
289
/// Note that if this is called _after_ an [`Observer`] is spawned, it will produce no effects.
282
290
pub fn watch_entity ( & mut self , entity : Entity ) {
283
291
self . descriptor . entities . push ( entity) ;
284
292
}
285
293
286
- /// Observe the given `component`. This will cause the [`Observer`] to run whenever the [`Event`] is triggered
294
+ /// Observes the given `entity` (in addition to any entity already being observed).
295
+ /// This will cause the [`Observer`] to run whenever the [`Event`] is triggered for any of these `entities`.
296
+ /// Note that if this is called _after_ an [`Observer`] is spawned, it will produce no effects.
297
+ pub fn watch_entities < I : IntoIterator < Item = Entity > > ( & mut self , entities : I ) {
298
+ self . descriptor . entities . extend ( entities) ;
299
+ }
300
+
301
+ /// Observes the given `component`. This will cause the [`Observer`] to run whenever the [`Event`] is triggered
287
302
/// with the given component target.
288
303
pub fn with_component ( mut self , component : ComponentId ) -> Self {
289
304
self . descriptor . components . push ( component) ;
290
305
self
291
306
}
292
307
293
- /// Observe the given `event_key`. This will cause the [`Observer`] to run whenever an event with the given [`EventKey`]
308
+ /// Observes the given `event_key`. This will cause the [`Observer`] to run whenever an event with the given [`EventKey`]
294
309
/// is triggered.
295
310
/// # Safety
296
311
/// The type of the `event_key` [`EventKey`] _must_ match the actual value
@@ -300,7 +315,7 @@ impl Observer {
300
315
self
301
316
}
302
317
303
- /// Set the error handler to use for this observer.
318
+ /// Sets the error handler to use for this observer.
304
319
///
305
320
/// See the [`error` module-level documentation](crate::error) for more information.
306
321
pub fn with_error_handler ( mut self , error_handler : fn ( BevyError , ErrorContext ) ) -> Self {
0 commit comments