[−][src]Struct bevy::prelude::Events
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> Events<T> where
T: Resource,
[src]
T: Resource,
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<Events<T>>)
[src]
A system that calls Events::update once per frame.
pub fn clear(&mut self)
[src]
Removes all events.
pub fn drain(&'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]
I: Iterator<Item = T>,
pub fn iter_current_update_events(&self) -> impl DoubleEndedIterator
[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> Any for T where
T: Any,
T: Any,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Component for T where
T: 'static + Send + Sync,
T: 'static + Send + Sync,
impl<T> Downcast for T where
T: Any,
T: Any,
fn into_any(self: Box<T>) -> Box<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
impl<T> DowncastSync for T where
T: Send + Sync + Any,
T: Send + Sync + Any,
impl<T> From<T> for T
[src]
impl<T> FromResources for T where
T: Default,
[src]
T: Default,
fn from_resources(_resources: &Resources) -> T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Resource for T where
T: 'static + Send + Sync,
[src]
T: 'static + Send + Sync,
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.
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.
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>,