Skip to content

Allow self transitioning states, or reentrant states #27

@Eitanski

Description

@Eitanski

I have this very simple code:

public enum GenericState
{
    Idle,
    Run,
    Jump
}

public enum Trigger
{
    Fire,
    Release
}
var sm = Fsm<GenericState, Trigger>.Builder(GenericState.Idle)
    .State(GenericState.Idle).TransitionTo(GenericState.Run).On(Trigger.Fire)
    .State(GenericState.Run).OnEnter(changeArgs => { Console.WriteLine("Entering run state"); })
    .TransitionTo(GenericState.Run).On(Trigger.Fire)
    .Build();

sm.Trigger(Trigger.Fire);
sm.Trigger(Trigger.Fire);

What I wish is that the state machine would be able to enter a state even though the target state is already the current state, and that the OnEnter method would run, which is not what happening here, as only one 'Entering run state' is being printed.

This is a very useful feature as it can simply reuse code for some state machines where there is simply a need for a reentering state. I encountered it myself: I have a character that is running based on mouse input, and when there is new mouse input and the character is already running, it needs to run again but towards the new mouse location, thereby requiring the OnEnter code to run again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions