There are 4 different node types and they are created by right clicking anywhere on the graph, apart from the Enter and Exit ones:
- The Enter node is unique and determines which State will be the first one when starting to run the graph.
- The Exit node is optional and allows the State Machine to finish running. Finishing running allows a State Machine graph to be used as a subgraph of another AI tool so it has a beginning and an end.
The State nodes are the backbone of a State Machine and is where the magic happens.
The State contains a Name field that allows giving the node a name. This has no effect on the execution and is just for information purposes.
The Conditions list determines whether this State can be transitioned to from another State with an edge pointing at this. If the conditions are not successful this State won't be transitioned to.
The Check field accepts two options:
- Every Cycle means that when this node is being executed, it will wait till its On Update instructions are completed before checking whether it can transition to another node.
- Every Frame means that when this node is being executed, it will check every frame if it can transition to another node.
Default to Every Cycle
It is tempting to check every frame whether the state should transition to another one. However, checking Conditions comes with a (very small) performance overhead. We recommend using Every Cycle when possible, so Conditions are called less frequently.
The On Enter instructions are called whenever this node starts being executed because of a transition.
The On Exit instructions are called whenever this node finishes executing and transitions to another node.
The On Update instructions are called every frame while the node is being executed, and will restart again automatically if the node is still being executed after finishing running the instructions.
Canceling On Update
It is important to node that the On Update instructions can be interrupted at any moment because of a transition. It's better to add the initialization instructions on the On Enter and the post-run instructions on the On Exit, which are guaranteed to be executed from start to finish.
Conditions serve as a gate to move from one state to another. If the conditions return false when attempting to switch states, the transition won't happen.
You might be wondering why there's a Conditions node when a State already has a Conditions list that do exactly the same thing.
This is because you might have multiple States that require the same Conditions and funnel them to a single output State node. In order to not repeating the same Conditions list on all States you can relay them to a single Conditions entry point.
Sub Graph nodes allow to encapsulate another graph as if it were a State.
It is important to node that a Sub Graph accepts any kind of graph, not just State Machine graphs. You can, for example, execute a Behavior Tree as a State.
Elbows don't do anything and just allow to improve the readability of graphs by creating corners.
Their shape is determined by the direction after dragging and dropping an edge from another node, but it can also be changed again by selecting the Elbow node and changing the direction in the Inspector.