Inspiaaa / UnityHFSM

A simple yet powerful class-based hierarchical finite state machine for Unity
MIT License
1.05k stars 121 forks source link

Fix KeyNotFoundException #5

Closed shiena closed 3 years ago

shiena commented 3 years ago

Prevents KeyNotFoundException when firing a trigger with no transition. The following code throws a KeyNotFoundException because there is no transition from "TO" to "TO".

using FSM;
using UnityEngine;

public class Foo : MonoBehaviour
{
    private StateMachine fsm;

    private void Awake()
    {
        fsm = new StateMachine(this);
        fsm.AddState("FROM", new State(needsExitTime: true));
        fsm.AddState("TO", new State(needsExitTime: true));
        fsm.AddTriggerTransition("TO", new Transition("FROM", "TO", forceInstantly: true));
        fsm.AddTriggerTransition("FROM", new Transition("TO", "FROM", forceInstantly: true));
        fsm.SetStartState("TO");
        fsm.Init();
    }

    void Trigger()
    {
        fsm.Trigger("TO");
    }

    private void Start()
    {
        Invoke(nameof(Trigger), 3);
    }

    private void Update()
    {
        fsm.OnLogic();
    }
}
shiena commented 3 years ago

I've resolved the conflicts.

Inspiaaa commented 3 years ago

Thanks for finding and solving the bug, and thanks for solving the merge conflict! I checked your proposal and it works. I will merge this pull request. 👍