Git Product home page Git Product logo

Comments (1)

Inspiaaa avatar Inspiaaa commented on May 23, 2024

Hi @Invertex, thanks for reaching out with your question.

I don't think what you've described is a bug. I've translated my understanding of your intention into the diagram below.

stateDiagram-v2
    Idle: Idle
    Movement: Movement (needsExitTime)

    state Movement {
        Walk
        Dodge: Dodge <br> (needsExitTime)

        [*] --> Walk
        Dodge --> Walk: Animation is finished <br> (forced)
        Walk --> Dodge: Dodge input
    }

    [*] --> Idle
    Idle --> Movement: Any input
    Movement --> Idle: No input

As far as I can tell, there are two distinct things that you want to happen:

  1. When inside the Movement state, the Dodge animation should finish before transitioning to the Walk state. That's why the Dodge state should have needsExitTime set to true. Because it needs exit time, it also needs a way to exit. As the custom onLogic function (Dodging) probably does not call the StateCanExit() method internally, the state transition needs to be forced from the outside, i.e. the transition needs forceInstantly to be true.

  2. When the root fsm wants to transition from Movement to Idle, it should not transition instantly if it's still playing the Dodge animation, i.e. the Dodge state has not exited yet. Therefore the Movement state (machine) also needs its own needsExitTime property set to true. The Movement state machine will then only exit, when its currently active state does not need exit time (like the Walk state) or when a state that does need exit time (like the Dodge state) exits.

The canExit field lets the state machine instantly transition out of a state that needs exit time. It will not be continually checked. In your case it does not make sense, as the Dodge state's needsExitTime property was set to false.

Without knowing the details of your code, I've tried to outline a solution in the code snippet below. Let me know if this helps.

// The root fsm does not needExitTime
StateMachine fsm = new StateMachine();

// Idle should be able to exit immediately => does not needExitTime.
fsm.AddState("Idle", new State(onLogic: Idle));

// As the movement state needs to remain in the active state as long
// as the animation needs to play, it needsExitTime.
StateMachine movementFsm = new StateMachine(needsExitTime: true);
fsm.AddState("Movement", movementFsm);

fsm.AddTransition("Idle", "Movement", _ => isAnyMovementInput);
fsm.AddTransition("Movement", "Input", _ => !isAnyMovementInput);

// The movementFsm state machine should be able to leave at any time
// if it is in the Walk state => Walk does not need exit time.
movementFsm.AddState("Walk", new State(onLogic: Walk, canExit: _ => isDodgeAnimationFinished));

// But as the Dodge animation has to finish before the state machine should ...
// a) transition to Walk, the Dodge state needsExitTime
// OR:
// b) exit and transition to Idle, the Dodge state needsExitTime AND
//    movementFsm needsExitTime.
movementFsm.AddState("Dodging", new State(onLogic: Dodging, needsExitTime: true));

// forceInstantly is required because the Dodge state needsExitTime and it
// won't exit itself (by calling movementFsm.StateCanExit()). That's why a
// transition has to be forced "from outside".
movementFsm.AddTransition(new Transition(
    "Dodge",
    "Walk",
    _ => isDodgeAnimationFinished,
    forceInstantly: true
));

// forceInstantly is not required here because Walk does not need exit time
movementFsm.AddTransition(new Transition(
    "Walk",
    "Dodge",
    _ => isPlayerPerformingDodge
));

from unityhfsm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.