#send-event #bevy #channel #events #gamedev #anywhere #c-ffi

bevy_channel_trigger

Send events via a channels form anywhere (eg. c-ffi) to Bevy Observers

5 releases

new 0.2.0 Nov 4, 2024
0.1.3 Nov 3, 2024
0.1.2 Nov 3, 2024
0.1.1 Nov 3, 2024
0.1.0 Nov 3, 2024

#800 in Game dev

Download history 99/week @ 2024-10-28

99 downloads per month
Used in bevy_web_popups

MIT/Apache

34KB
59 lines

bevy_channel_trigger

Following released Bevy versions crates.io docs.rs

Send events via a channel from anywhere (eg. web-dom, c-ffi) to Bevy Observers. Inspired by bevy_crossbeam_event but delivering the events via Bevy Observers instead of EventReader.

example

#[derive(Event)]
struct MyEvent(i32);

fn main() {
    use bevy_channel_trigger::ChannelTriggerApp;

    let mut app = App::new();
    app.add_plugins(MinimalPlugins);

    // create channel
    let sender = app.add_channel_trigger::<MyEvent>();

    // use sender from anywhere:
    thread::spawn(move || {
        let mut counter = 1;
        loop {
            // send events back to bevy
            sender.send(MyEvent(counter));
            thread::sleep(Duration::from_secs(1));
            counter += 1;
        }
    });

    // register an observer to receive the events sent via `sender`
    app.observe(on_event);

    app.run();
}

// regular bevy observer to handle these events coming into the bevy world
fn on_event(trigger: Trigger<MyEvent>) {
    let event = trigger.event();
    println!("trigger with: {}", event.0);
}

Other examples

Our Other Crates

Compatible Bevy Versions

bevy our version
0.14 0.1

License

this crate is dual-licensed under either MIT or Apache 2.0, at your option.

Dependencies

~25MB
~464K SLoC