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

observable_event

crates.io ci codecov license

The EventObservable turns Bevy events triggered on an entity into signals, allowing you to use any event as an observable source, and construct reactive pipelines from them using operators.

Subscribers will observe events targeted at the specified entity, and a completion signal once the entity is despawned.

See Also

Example

cargo run -p rx_bevy --example observable_event_example
#[derive(Resource, Deref, DerefMut)]
pub struct DummyEventTarget(Entity);

#[derive(Resource, Default, Deref, DerefMut)]
pub struct ExampleSubscriptions(SharedSubscription);

fn setup(
    mut commands: Commands,
    rx_schedule_update_virtual: RxSchedule<Update, Virtual>,
    mut subscriptions: ResMut<ExampleSubscriptions>,
) {
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(2., 6., 8.).looking_at(Vec3::ZERO, Vec3::Y),
    ));

    let watched_entity = commands.spawn(Name::new("Watch me")).id();

    subscriptions.add(
        EventObservable::<DummyEvent>::new(watched_entity, rx_schedule_update_virtual.handle())
            .subscribe(PrintObserver::new("event_observable")),
    );

    commands.insert_resource(DummyEventTarget(watched_entity));
}

Then, provided something is triggering DummyEvents on the watched entity:

Producer is sending DummyEvent { target: 6v1#4294967302 } to 6v1!
event_observable - next: DummyEvent { target: 6v1#4294967302 }
Producer is sending DummyEvent { target: 6v1#4294967302 } to 6v1!
event_observable - next: DummyEvent { target: 6v1#4294967302 }
...