[][src]Struct bevy_app::Events

pub struct Events<T> { /* fields omitted */ }

An event collection that represents the events that occurred within the last two Events::update calls. Events can be cheaply read using an EventReader. 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. EventReaders are expected to read events from this collection at least once per update/frame. If events are not handled within one frame/update, they will be dropped.

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. However this complicates consumption

Implementations

impl<T: Resource> 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) -> EventReader<T>[src]

Gets a new EventReader. This will include all events already in the event buffers.

pub fn get_reader_current(&self) -> EventReader<T>[src]

Gets a new EventReader. 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<'a>(&'a mut self) -> impl Iterator<Item = T> + 'a[src]

Creates a draining iterator that removes all events.

pub fn extend<I>(&mut self, events: I) where
    I: Iterator<Item = T>, 
[src]

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

impl<T: Debug> Debug for Events<T>[src]

impl<T> Default for Events<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Events<T> where
    T: RefUnwindSafe

impl<T> Send for Events<T> where
    T: Send

impl<T> Sync for Events<T> where
    T: Sync

impl<T> Unpin for Events<T> where
    T: Unpin

impl<T> UnwindSafe for Events<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Component for T where
    T: 'static + Send + Sync

impl<T> From<T> for T[src]

impl<T> FromResources for T where
    T: Default
[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Resource for T where
    T: 'static + Send + Sync
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,