#events #bevy #bevy-plugin #gamedev

bevy_extern_events

Bevy plugin for generic external events to be send to bevy EventReaders

8 releases

0.3.0 Nov 27, 2023
0.2.3 Nov 24, 2023
0.1.2 Nov 24, 2023

#1772 in Game dev

Download history 12/week @ 2024-02-19 5/week @ 2024-02-26 4/week @ 2024-03-11 86/week @ 2024-04-01

86 downloads per month

MIT license

9KB
62 lines

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

Dependencies

~18–27MB
~408K SLoC