#How to use `on_event` of seldom_state

2 messages · Page 1 of 1 (latest)

white pivot
#

@vast blade Hi, I am using your tool seldom_state.
Thank you for developing cool tool!

Is there a document or sample code how to use on_event trigger?
I want to use this trigger like .trans::<AnyEvent, _>(on_event(...?), GotEventState), but i do not now what value to pass.

I wrote like a following code:

pub fn setup(
    mut commands: Commands,
    mut ev_state_event: EventReader<StateEvent>,
) {
    commands.spawn((      
        ... some inserts ...
        StateMachine::default()
        .trans::<AnyState, _>(on_event(&mut ev_state_event), GotEventState)           
    );    
))

This is compile error message.

error[E0277]: the trait bound `std::option::Option<StateEvent>: IntoTrigger<_>` is not satisfied
   --> src/js.rs:414:39
    |
414 |                 .trans::<AnyState, _>(on_event(ev_state_event), GotEventState)
    |                  -----                ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoTrigger<_>` is not implemented for `std::option::Option<StateEvent>`
    |                  |
    |                  required by a bound introduced by this call
    |
vast blade
#

Like so:

.trans::<AnyState, _>(on_event::<StateEvent>, GotEventState)

There's actually a bug with on_event rn that I haven't fixed yet. It can read events from before it transitioned to that state. So if that's a problem for you, you can something like this instead:

pub fn on_event<T: Clone + Event>(mut has_run: Local<bool>, mut reader: EventReader<T>) -> Option<T> {
    if !*has_run {
        // Drain old events
        reader.read().last();
        *has_run = true;
        return None;
    }

    reader.read().last().cloned()
}