diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index fb36921bafe7d..2bbec2aaa9e85 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1156,6 +1156,30 @@ impl World { result } + /// Sends an [`Event`](crate::event::Event). + #[inline] + pub fn send_event(&mut self, event: E) { + self.send_event_batch(std::iter::once(event)); + } + + /// Sends the default value of the [`Event`](crate::event::Event) of type `E`. + #[inline] + pub fn send_default_event(&mut self) { + self.send_event_batch(std::iter::once(E::default())); + } + + /// Sends a batch of [`Event`](crate::event::Event)s from an iterator. + #[inline] + pub fn send_event_batch(&mut self, events: impl Iterator) { + match self.get_resource_mut::>() { + Some(mut events_resource) => events_resource.extend(events), + None => bevy_utils::tracing::error!( + "Unable to send event `{}`\n\tEvent must be added to the app with `add_event()`\n\thttps://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ", + std::any::type_name::() + ), + } + } + /// # Safety /// `component_id` must be assigned to a component of type `R` #[inline]