How inspiaaa/unityhfsm works
Main components
• Core State Machine Engine – Represented in the src/StateMachine folder. This is the heart of the library that manages state transitions, activation, exit requests, and global versus local transitions. It also extends the base state class so that nested state machines can be used as states.
• Base Interfaces and Abstract Classes – Located in src/Base. They define essential interfaces such as IActionable, IStateMachine, ITriggerable, IStateTimingManager, and provide abstract classes like StateBase and TransitionBase that other components inherit from.
• State Implementations – Found under src/States. This includes built-in implementations such as ActionState, CoState (for coroutine support), DecoratedState, and ParallelStates – which show that the framework supports both standard and advanced (coroutine-based) states.
• Transition Implementations – In src/Transitions. Here you have classes like Transition, ReverseTransition, TransitionAfter, TransitionOnKey, and TransitionOnMouse, which encapsulate the rules for moving from one state to another (both through polling and event-based mechanisms).
• Exception Handling – In src/Exceptions. These classes (e.g., StateMachineException and related helpers) propagate detailed error information to assist developers debugging state configuration errors.
• Inspection and Debugging – In src/Inspection. Interfaces and utility classes (like IStateMachineHierarchyVisitor, StateMachinePath, StateMachineWalker) support diagnosing hierarchical state machine paths, essential for debugging.
• Utilities – Under src/Util. These provide necessary timing functionality (ITimer, Timer) that support “exit time” handling and delayed transitions.
• Visualization – In src/Visualization. This part facilitates integration with Unity’s Animator Graph (e.g., HfsmAnimatorGraph) to visually represent the state machine for runtime debugging.
• Documentation and Sample Projects – The Samples and docs directories include example scenes (like EnemyAI scenarios) and flowcharts that explain the intended use of the HFSM framework.
Relationships and interactions
• The Core State Machine Engine is central and directly interacts with State and Transition components. It processes OnEnter, OnLogic, and OnExit calls and handles the main control flow.
• States (both simple and hierarchical) maintain custom logic via lambda expressions or dedicated classes. They can themselves be state machines, showing a nested relationship.
• Transitions (both polling-based and trigger/event-based) evaluate conditions and, when met (potentially with support from exit conditions), signal the state machine to change states.
• Exception handlers intercept and format errors during state transitions.
• Debug and inspection components can traverse or extract current hierarchical paths for logging and visualization.
• Utility classes (like Timer) are used by states and transitions to implement delayed or controlled state changes.
Architectural Patterns and Design Principles
• The design follows an object oriented pattern with a clear separation of concerns. There are distinct modules for state management, transition logic, debugging, visualization, and exception handling.
• Hierarchical composition is a core principle – state machines can be nested, and “ghost states” allow for immediate multi-transition flows.
• Generics are used to provide type safety (avoiding the pitfalls of using plain strings) and to improve performance.
• It combines both polling-based and event-based transition models.
• The framework is made extensible by allowing custom state and transition classes to be derived from common base classes and interfaces.