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

[Bug] Statemachines inside parallel states don't react to global triggers #48

Closed
TdXYZ opened this issue Jun 1, 2024 · 2 comments
Closed

Comments

@TdXYZ
Copy link
Contributor

TdXYZ commented Jun 1, 2024

Given the following statemachine configuration:

private StateMachine _stateMachine;

void Start()
{
    StateMachine a = new();
    a.AddState("A");
    a.AddState("B");
    a.AddTriggerTransition("T", "A", "B");
    a.AddTriggerTransition("T", "B", "A");

    StateMachine b = new();
    b.AddState("C");
    b.AddState("D");
    b.AddTriggerTransition("T", "C", "D");
    b.AddTriggerTransition("T", "D", "C");

    _stateMachine = new StateMachine();
    _stateMachine.AddState("root", new ParallelStates(a, b));
    _stateMachine.Init();
}

Using _statemachine.Trigger("T") will not cause a and b to perform their transitions. This unfortunately makes ParallelStates useless for statemachines that rely heavily on trigger transitions.

This can be fixed by having ParallelStates implement ITriggereable. There is however a potential issue that should be addressed. Say that statemachine "a" has some nested statemachines and statemachine "b" has a transition whose guard checks ActiveStateName of one of those nested machines. An Exception will be thrown if the nested machine is not the active state of its parent machine.

Inspiaaa added a commit that referenced this issue Mar 21, 2025
Fixes a potential bug mentioned in #48 by checking if the state is still active before calling certain methods on sub-states.
@Inspiaaa
Copy link
Owner

Hi @TdXYZ,

Nice catch! Thanks for providing a solution in your PR.

As far as I can see, the secondary issue that you highlighted is not only limited to the Trigger method, but also to other methods that are called on sub-states that may lead to a transition, such as OnLogic.

I have implemented a solution for all these potential problems now. It checks the isActive field so that it can avoid calling certain methods after exiting.

It is already available in the release branch. For reference, I am going to keep this issue open until the next version of UnityHFSM is released.

@Inspiaaa
Copy link
Owner

Version 2.2 has just been released 🚀

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

2 participants