Skip to content

extrawurst/bevy_extern_events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bevy_extern_events

crates.io docs

Why?

Because at some point you might want to interact with code outside of Bevy (External SDKs, Native Platform Code, non-Bevy crates). With the help of this crate you can queue events from anywhere and they will be available via the typical EventReader mechanism inside your Bevy Systems.

Note that this comes at the cost of us having a global static RwLock-based Queue that we poll every frame (PreUpdate) to forward into an EventWriter. Events are Boxed because I found no other way of having a global static generic Datatype without using Any.

Therefore I suggest using this for non-every-frame interaction and rolling a custom solution otherwise.

Example:

#[derive(Default)]
pub struct MyEvent;

#[derive(Resource, Reflect, Default)]
pub struct MyEventResource(i32);

pub fn event_system(
    mut res: ResMut<MyEventResource>,
    mut native_events: EventReader<ExternEvent<MyEvent>>,
) {
    for _e in native_events.read() {
        res.0 += 1;
    }
}

fn test() {
    let mut app = App::new();
    app.init_resource::<MyEventResource>()
        .add_plugins(ExternEventsPlugin::<MyEvent>::default())
        .add_systems(Update, event_system);

    queue_event(MyEvent::default());

    app.update();

    assert_eq!(app.world.resource::<MyEventResource>().0, 1);
}

TODO

  • CI
  • clippy

About

Bevy plugin for generic external events forwarded to Bevy EventReaders 🦀

Topics

Resources

Stars

Watchers

Forks

Languages