States
A robust and flexible state machine implementation for Unity
In One Line
States that flow like water, solid as rock.
Overview
The States Package is designed to manage complex state transitions and behaviors in your game objects and systems. It provides a clean, builder-pattern-based API for creating and managing state machines with support for nested states and system integration.
Package Info
display name
AceLand States
package name
latest version
1.0.10
namespace
git repository
dependencies
Key Features
Builder pattern for intuitive state machine construction
Support for any-state transitions
Sub state machine capabilities
System integration through Unity's PlayerLoop
Automatic resource management and cleanup
Asynchronous state machine retrieval
Custom state action injection
Debug logging support
Core Components
StateMachine
The main class for creating and managing states. Provides:
State transitions management
Update cycle handling
Resource cleanup
System integration capabilities
State
Represents individual states within the state machine:
Customizable Enter/Update/Exit behaviors
Sub State Machine support
Action injection capabilities
Unique state identification
How It Works
Core Architecture
The States Package operates on a hierarchical system with several key components working together:
State Machine Core
Maintains a registry of all states
Manages transitions between states
Handles the update cycle
Controls state lifecycle (Enter → Update → Exit)
State Transitions
Uses a double-buffered transition system to prevent race conditions
Evaluates transitions in priority order:
Any-state transitions
Specific state transitions
Prevents invalid transitions through state validation
Performance Considerations
Update Optimization
Minimal garbage collection impact
Cached transition evaluations
Efficient state lookup through dictionary storage
Memory Efficiency
States are shared when possible
Action delegates are cached
Transition conditions are optimized
Usage Example
Create State Machine Ids
Create StateMachine [Test1]
Create StateMachine [Test2] and inject to State Running in [Test1]
Best Practices
Always dispose of state machines when no longer needed
Use meaningful state names for better debugging
Implement transition conditions as pure functions
Utilize sub state machines for complex state hierarchies
Consider using system integration for performance-critical scenarios
Last updated