​Plugin-Based State Machine Framework with a Visual Scripting Tool
Published 2 years ago
Plugin-Based State Machine Framework with a Visual Scripting Tool
​This project started out as an attempt to make a 2D souls-like game in Unity and evolved into me trying to create a easily modifiable state machine framework.
The framework uses a central state class from which specific states can be derived. States mainly to drive animation, the logic to transition between states would exist separate from the state in a list of "Conditions". This is where the plug-in aspect comes in, conditions can be created by deriving from the base Condition class which is a Unity scriptable object. This allows a designer to plug-in different conditions and states to create any type of behavior for either the player or AI.
As work continued, I realized it was getting hard to track the state transitions in the Unity editor, so I decided to create a visual representation of the states and their transitions. The screenshot below shows what the prototype of the visualizer looks like.
As I was building the visualizer I started to think about a full fledged state machine editor that would let me do what I do in the inspector in an easier way.
I knew that a good UI to emulate would be the Blueprint system from Unreal Engine.
The first step was to let the user add a state to an object, the natural way to do this was by right clicking and providing the user a context menu.
The obvious second step was to allow the user to add transitions to a state. However, the transition can have properties and I had to allow those to be edited also. I solved this problem by adding a button that would add a transition to the state and update it in the state window. Clicking on the transition will open up another window on the side that will show the properties that you can edit. Adding a condition to a transition will cause it's name to show up on the transition.
The final step is to connect two states. I used the in built "DrawBerzier" function to represent the connection, the connection goes from the output of a transition to the input of the state it transitions to.
In the above example we've configures it so that the Idle state transitions to the Walk state when the "PlayerWalk" condition is true. To complete the walk cycle I'll connect the Walk state back to the Idle state when the same condition is false.
Now we can see the state machine working!
You can download the project source code from the link below.
Bharat Gudihal
Engineer - Programmer