aevyrie / bevy_eventlistener

Event listening, bubbling, and callbacks
Apache License 2.0
178 stars 27 forks source link

Add `On::set_next_state` #26

Open alice-i-cecile opened 7 months ago

alice-i-cecile commented 7 months ago

This is a common simple pattern that uses a surprising amount of boilerplate. We could simplify this to a single method call.

alice-i-cecile commented 7 months ago

A simple working prototype:

    fn set_next_state<S: States + Clone>(next_state: S) -> Self {
        On::run(move |mut state: ResMut<NextState<S>>| {
            state.set(next_state.clone());
        })
    }

The Clone bound is required as NextState::set takes ownership, and we need to be able to reuse the callback.

aevyrie commented 6 months ago

You might want to change this to take a closure instead, like mentioned here: https://github.com/aevyrie/bevy_eventlistener/issues/25

That would avoid the need for Clone, and you would be able to react to the contents of the event that is causing a state change.

set_next_state<S: States>(impl FnMut(E) -> S) -> Self