use bevy_ecs::{1event::{BufferedEvent, EventId, Events, WriteBatchIds},2system::{ResMut, SystemParam},3};45/// Writes [`BufferedEvent`]s of type `T`.6///7/// # Usage8///9/// `EventWriter`s are usually declared as a [`SystemParam`].10/// ```11/// # use bevy_ecs::prelude::*;12///13/// #[derive(BufferedEvent)]14/// pub struct MyEvent; // Custom event type.15/// fn my_system(mut writer: EventWriter<MyEvent>) {16/// writer.write(MyEvent);17/// }18///19/// # bevy_ecs::system::assert_is_system(my_system);20/// ```21/// # Observers22///23/// "Buffered" events, such as those sent directly in [`Events`] or written using [`EventWriter`], do _not_ automatically24/// trigger any [`Observer`]s watching for that event, as each [`BufferedEvent`] has different requirements regarding _if_ it will25/// be triggered, and if so, _when_ it will be triggered in the schedule.26///27/// # Concurrency28///29/// `EventWriter` param has [`ResMut<Events<T>>`](Events) inside. So two systems declaring `EventWriter<T>` params30/// for the same event type won't be executed concurrently.31///32/// # Untyped events33///34/// `EventWriter` can only write events of one specific type, which must be known at compile-time.35/// This is not a problem most of the time, but you may find a situation where you cannot know36/// ahead of time every kind of event you'll need to write. In this case, you can use the "type-erased event" pattern.37///38/// ```39/// # use bevy_ecs::{prelude::*, event::Events};40/// # #[derive(BufferedEvent)]41/// # pub struct MyEvent;42/// fn write_untyped(mut commands: Commands) {43/// // Write an event of a specific type without having to declare that44/// // type as a SystemParam.45/// //46/// // Effectively, we're just moving the type parameter from the /type/ to the /method/,47/// // which allows one to do all kinds of clever things with type erasure, such as sending48/// // custom events to unknown 3rd party plugins (modding API).49/// //50/// // NOTE: the event won't actually be sent until commands get applied during51/// // apply_deferred.52/// commands.queue(|w: &mut World| {53/// w.write_event(MyEvent);54/// });55/// }56/// ```57/// Note that this is considered *non-idiomatic*, and should only be used when `EventWriter` will not work.58///59/// [`Observer`]: crate::observer::Observer60#[derive(SystemParam)]61pub struct EventWriter<'w, E: BufferedEvent> {62#[system_param(validation_message = "BufferedEvent not initialized")]63events: ResMut<'w, Events<E>>,64}6566impl<'w, E: BufferedEvent> EventWriter<'w, E> {67/// Writes an `event`, which can later be read by [`EventReader`](super::EventReader)s.68/// This method returns the [ID](`EventId`) of the written `event`.69///70/// See [`Events`] for details.71#[doc(alias = "send")]72#[track_caller]73pub fn write(&mut self, event: E) -> EventId<E> {74self.events.write(event)75}7677/// Writes a list of `events` all at once, which can later be read by [`EventReader`](super::EventReader)s.78/// This is more efficient than writing each event individually.79/// This method returns the [IDs](`EventId`) of the written `events`.80///81/// See [`Events`] for details.82#[doc(alias = "send_batch")]83#[track_caller]84pub fn write_batch(&mut self, events: impl IntoIterator<Item = E>) -> WriteBatchIds<E> {85self.events.write_batch(events)86}8788/// Writes the default value of the event. Useful when the event is an empty struct.89/// This method returns the [ID](`EventId`) of the written `event`.90///91/// See [`Events`] for details.92#[doc(alias = "send_default")]93#[track_caller]94pub fn write_default(&mut self) -> EventId<E>95where96E: Default,97{98self.events.write_default()99}100}101102103