diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index f79bc6d52a..07a29800f9 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -990,6 +990,36 @@ impl App { } /// Spawns an [`Observer`] entity, which will watch for and respond to the given event. + /// + /// # Examples + /// + /// ```rust + /// # use bevy_app::prelude::*; + /// # use bevy_ecs::prelude::*; + /// # use bevy_utils::default; + /// # + /// # let mut app = App::new(); + /// # + /// # #[derive(Event)] + /// # struct Party { + /// # friends_allowed: bool, + /// # }; + /// # + /// # #[derive(Event)] + /// # struct Invite; + /// # + /// # #[derive(Component)] + /// # struct Friend; + /// # + /// // An observer system can be any system where the first parameter is a trigger + /// app.observe(|trigger: Trigger, friends: Query>, mut commands: Commands| { + /// if trigger.event().friends_allowed { + /// for friend in friends.iter() { + /// commands.trigger_targets(Invite, friend); + /// } + /// } + /// }); + /// ``` pub fn observe( &mut self, observer: impl IntoObserverSystem, diff --git a/crates/bevy_ecs/src/system/observer_system.rs b/crates/bevy_ecs/src/system/observer_system.rs index c5a04f25dd..28ea902b1a 100644 --- a/crates/bevy_ecs/src/system/observer_system.rs +++ b/crates/bevy_ecs/src/system/observer_system.rs @@ -25,6 +25,11 @@ impl< } /// Implemented for systems that convert into [`ObserverSystem`]. +#[diagnostic::on_unimplemented( + message = "`{Self}` cannot become an `ObserverSystem`", + label = "the trait `IntoObserverSystem` is not implemented", + note = "for function `ObserverSystem`s, ensure the first argument is a `Trigger` and any subsequent ones are `SystemParam`" +)] pub trait IntoObserverSystem: Send + 'static { /// The type of [`System`] that this instance converts into. type System: ObserverSystem;