Struct bevy_app::Events [−][src]
An event collection that represents the events that occurred within the last two
Events::update
calls.
Events can be written to using an EventWriter
and are typically cheaply read using an EventReader
.
Each event can be consumed by multiple systems, in parallel,
with consumption tracked by the EventReader
on a per-system basis.
This collection is meant to be paired with a system that calls
Events::update
exactly once per update/frame.
Events::update_system
is a system that does this, typically intialized automatically using
[AppBuilder::add_event
]. EventReaders are expected to read events from this collection at
least once per loop/frame.
Events will persist across a single frame boundary and so ordering of event producers and
consumers is not critical (although poorly-planned ordering may cause accumulating lag).
If events are not handled by the end of the frame after they are updated, they will be
dropped silently.
Example
use bevy_app::Events; struct MyEvent { value: usize } // setup let mut events = Events::<MyEvent>::default(); let mut reader = events.get_reader(); // run this once per update/frame events.update(); // somewhere else: send an event events.send(MyEvent { value: 1 }); // somewhere else: read the events for event in reader.iter(&events) { assert_eq!(event.value, 1) } // events are only processed once per reader assert_eq!(reader.iter(&events).count(), 0);
Details
Events is implemented using a double buffer. Each call to Events::update swaps buffers and clears out the oldest buffer. EventReaders that read at least once per update will never drop events. EventReaders that read once within two updates might still receive some events. EventReaders that read after two updates are guaranteed to drop all events that occurred before those updates.
The buffers in Events will grow indefinitely if Events::update is never called.
An alternative call pattern would be to call Events::update manually across frames to control
when events are cleared.
This complicates consumption and risks ever-expanding memory usage if not cleaned up,
but can be done by adding your event as a resource instead of using [AppBuilder::add_event
].
Implementations
impl<T: Component> Events<T>
[src]
pub fn send(&mut self, event: T)
[src]
“Sends” an event
by writing it to the current event buffer. EventReaders can then read
the event.
pub fn get_reader(&self) -> ManualEventReader<T>
[src]
Gets a new ManualEventReader. This will include all events already in the event buffers.
pub fn get_reader_current(&self) -> ManualEventReader<T>
[src]
Gets a new ManualEventReader. This will ignore all events already in the event buffers. It will read all future events.
pub fn update(&mut self)
[src]
Swaps the event buffers and clears the oldest event buffer. In general, this should be called once per frame/update.
pub fn update_system(events: ResMut<'_, Self>)
[src]
A system that calls Events::update once per frame.
pub fn clear(&mut self)
[src]
Removes all events.
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_
[src]
Creates a draining iterator that removes all events.
pub fn extend<I>(&mut self, events: I) where
I: Iterator<Item = T>,
[src]
I: Iterator<Item = T>,
pub fn iter_current_update_events(&self) -> impl DoubleEndedIterator<Item = &T>
[src]
Iterates over events that happened since the last “update” call.
WARNING: You probably don’t want to use this call. In most cases you should use an
EventReader
. You should only use this if you know you only need to consume events
between the last update()
call and your call to iter_current_update_events
.
If events happen outside that window, they will not be handled. For example, any events that
happen after this call and before the next update()
call will be dropped.
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Events<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for Events<T> where
T: Send,
T: Send,
impl<T> Sync for Events<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Events<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Events<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Component for T where
T: 'static + Send + Sync,
[src]
T: 'static + Send + Sync,
impl<T> Downcast for T where
T: Any,
T: Any,
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
pub fn as_any(&self) -> &(dyn Any + 'static)
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
impl<T> DowncastSync for T where
T: Any + Send + Sync,
T: Any + Send + Sync,
impl<T> From<T> for T
[src]
impl<T> FromWorld for T where
T: Default,
[src]
T: Default,
pub fn from_world(_world: &mut World) -> T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,