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

Fix ParallelStates not reacting to global triggers #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Tests/TestParallelStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,28 @@ public void Test_ps_child_state_can_use_different_type_for_id()
fsm.Init();
fsm.OnLogic();
}

[Test]
public void Test_ps_reacts_to_global_trigger()
{
var a = new StateMachine();
a.AddState("A");
a.AddState("B");
a.AddTriggerTransition("T", "A", "B");

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

fsm.AddState("root", new ParallelStates(a, b));
fsm.Init();

fsm.Trigger("T");
Assert.AreEqual("B", a.ActiveStateName);
Assert.AreEqual("D", b.ActiveStateName);


}
}
}
10 changes: 9 additions & 1 deletion src/States/ParallelStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace UnityHFSM
/// This will ignore the needsExitTime and StateCanExit() calls of the child states. It works the same as the
/// canExit feature of the State class.
/// </remarks>
public class ParallelStates<TOwnId, TStateId, TEvent> : StateBase<TOwnId>, IActionable<TEvent>, IStateMachine
public class ParallelStates<TOwnId, TStateId, TEvent> : StateBase<TOwnId>, IActionable<TEvent>, IStateMachine, ITriggerable<TEvent>
{
private List<StateBase<TStateId>> states = new List<StateBase<TStateId>>();

Expand Down Expand Up @@ -185,6 +185,14 @@ public void StateCanExit()
fsm.StateCanExit();
}
}

public void Trigger(TEvent trigger)
{
foreach (var state in states)
{
(state as ITriggerable<TEvent>)?.Trigger(trigger);
}
}

public override string GetActiveHierarchyPath()
{
Expand Down