Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

wait::event

The wait::event module provides actions for waiting to receive Bevy events. These actions are useful for creating tasks that respond to events in your game or application.

Functions

comes

wait::event::comes<E>() -> ActionSeed

Creates an action that waits until an event of type E is received. The action completes when the event is received but does not return the event itself.

Example

#![allow(unused)]
fn main() {
use bevy::app::AppExit;
use bevy::prelude::*;
use bevy_flurx::prelude::*;

Reactor::schedule(|task| async move {
    // Wait for an AppExit event
    task.will(Update, wait::event::comes::<AppExit>()).await;
    
    // This code runs after an AppExit event is received
    println!("App is exiting!");
});
}

comes_and

wait::event::comes_and<E>(predicate: impl Fn(&E) -> bool + Send + Sync + 'static) -> ActionSeed

Creates an action that waits until an event of type E is received and the event matches the given predicate. The action completes when a matching event is received but does not return the event itself.

Example

#![allow(unused)]
fn main() {
use bevy::app::AppExit;
use bevy::prelude::*;
use bevy_flurx::prelude::*;

Reactor::schedule(|task| async move {
    // Wait for a successful AppExit event
    task.will(Update, wait::event::comes_and::<AppExit>(|e| {
        e.is_success()
    })).await;
    
    // This code runs after a successful AppExit event is received
    println!("App is exiting successfully!");
});
}

read

wait::event::read<E>() -> ActionSeed<(), E>

Creates an action that waits until an event of type E is received and returns a clone of the event. This is similar to comes, but it returns the event itself.

Example

#![allow(unused)]
fn main() {
use bevy::app::AppExit;
use bevy::prelude::*;
use bevy_flurx::prelude::*;

Reactor::schedule(|task| async move {
    // Wait for an AppExit event and get the event
    let exit_event = task.will(Update, wait::event::read::<AppExit>()).await;
    
    // This code runs after an AppExit event is received
    println!("App is exiting with status: {:?}", exit_event);
});
}

read_and

#![allow(unused)]
fn main() {
wait::event::read_and<E>(predicate: impl Fn(&E) -> bool + Send + Sync + 'static) -> ActionSeed<(), E>
}

Creates an action that waits until an event of type E is received, the event matches the given predicate, and returns a clone of the event. This is similar to comes_and, but it returns the event itself.

Example

#![allow(unused)]
fn main() {
use bevy::app::AppExit;
use bevy::prelude::*;
use bevy_flurx::prelude::*;

Reactor::schedule(|task| async move {
    // Wait for a successful AppExit event and get the event
    let exit_event = task.will(Update, wait::event::read_and::<AppExit>(|e| {
        e.is_success()
    })).await;
    
    // This code runs after a successful AppExit event is received
    println!("App is exiting successfully with event: {:?}", exit_event);
});
}

When to Use

Use wait::event actions when you need to:

  • Wait for specific events to occur before continuing execution
  • React to events in an asynchronous manner
  • Filter events based on their content using predicates
  • Retrieve event data for further processing

For more complex event handling scenarios, consider combining wait::event with other wait actions like wait::either or wait::any to wait for multiple different event types.