Seldom-SE / seldom_state

Component-based state machine plugin for Bevy. Useful for AI, player state, and other entities that occupy different states.
Apache License 2.0
172 stars 17 forks source link

Access to an entity in the transition builder #11

Open soanvig opened 1 week ago

soanvig commented 1 week ago

It would be nice to have access to an entity the state machine is assigned to in the .trans_builder.

For example - I can react to an event, and switch a state, but only if the event corelates with the entity. The event carries over the information about the entity in one of its fields. The state machine however has access only to its current state, so unless I store the entity in the state, I cannot compare entity in the event, and entity that uses the state machine.

I think technically there is no reason to not provide an entity to the .trans_builder, am I right?

I can help with a PR

Seldom-SE commented 1 week ago

Would it be sufficient in your use case to get the entity ID when spawning it?

let mut my_entity = commands.spawn_empty();
let my_entity_id = my_entity.id();
my_entity.insert(
    StateMachine::default()
        .trans_builder(MyTrigger, move |my_state: &MyState, another_entity| {
            (my_entity != another_entity).then_some(MyOtherState)
        })
);
soanvig commented 1 week ago

Surely. Thanks for the workaround. It reduces flexibility (I cannot >easily< have state machine factory), but it works.