Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow self transitioning states, or reentrant states #27

Open
Eitanski opened this issue Jul 20, 2024 · 0 comments
Open

Allow self transitioning states, or reentrant states #27

Eitanski opened this issue Jul 20, 2024 · 0 comments

Comments

@Eitanski
Copy link

Eitanski commented Jul 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant