mdeloof / statig

Hierarchical state machines for designing event-driven systems
https://crates.io/crates/statig
MIT License
560 stars 18 forks source link

Problem with using StateMachine as Bevy Component #17

Closed DylanRJohnston closed 6 months ago

DylanRJohnston commented 6 months ago

Hey all, I noticed the Bevy feature flag when installing statig which allows StateMachine to implement Component however I can't seem to get this to work correctly. I get the error

the trait bound `statig::prelude::StateMachine<monster::Behaviour>: bevy::prelude::Component` is not satisfied
the following other types implement trait `bevy::prelude::Component`:
  animation_link::AnimationLink
...

Code snippets

#[derive(Bundle)]
pub struct MonsterBundle {
    pub scene: SceneBundle,
    pub speed: Speed,
    pub stats: Stats,
    pub animations: NamedAnimations,
    pub behaviour: StateMachine<Behaviour>,
}

#[derive(Default)]
pub struct Behaviour;

pub enum Event {
    TimerElapsed,
    Start,
    Stop,
    Dance,
}

#[state_machine(initial = "State::idle()")]
impl Behaviour {
    #[state(entry_action = "enter_idle")]
    fn idle(event: &Event) -> Response<State> {
...

Wrapping it in a newtype seems to work correctly

#[derive(Component)]
pub struct BehaviourState(pub StateMachine<Behaviour>);
DylanRJohnston commented 6 months ago

It seems this is just a problem with a mismatch in the bevy_ecs version.

mdeloof commented 6 months ago

Solved with #18