Traversing a State Machine¶
It is possible to manually traverse a state machine through blueprints or C++ by going through connected or contained nodes. This can be useful for various reasons, such as if you want to build out a map of information about your state machine, or if you are creating a dialogue system on Pro and want to examine connected choices.
The Main Instance¶
First, get access to your main blueprint USMInstance you have instantiated.
- If you are using a component you will need to call GetInstance() on it.
- From a state machine reference you can call GetPrimaryReferenceOwner() if you want the top most instance.
Iterating Through Nodes¶
Getting the Root State Machine¶
From the USMInstance
you can call GetRootStateMachineNodeInstance().
- State machines are just states that can contain states. So we will want to both check contained states as well as connected states.
State Processing¶
You probably want to create your own function for processing a state. Whether it's to continue iteration or retrieve information from it relative to your project.
If you give the function an input parameter of USMStateInstance_Base this can accept states, state machines, and conduits. From here you can cast to the type you are expecting and perform specific processing, such as casting to an USMStateMachineInstance and checking entry states.
Entry States¶
If the state you are processing is a state machine, you can can call GetEntryStates() returning the entry state(s) to your state machine. Iterate this array and process each state.
Connected states¶
From a state we can call GetOutgoingTransitions(). This will return an array of all outbound transitions. We can iterate this array calling GetNextStateInstance() on each transition instance. Pass this state instance back to your state processing function.
Getting All Nodes¶
If you don't care about the specific order nodes are connected, you can simply retrieve all nodes.
From your main USMInstance
you can call GetAllStateInstances() and GetAllTransitionInstances(). These are flattened arrays of all nodes within the state machine, including nested references. This is the most efficient way to retrieve all contained nodes.
Owning Nodes¶
To traverse up the state machine instead, call GetOwningStateMachineNodeInstance() which will return the owning state machine node.
Manually Switching States and Evaluating Transitions¶
Evaluate a Transition¶
Transition instances have a DoesTransitionPass() method which is similar to what Logic Driver calls internally when evaluating transitions. This will evaluate your conditional result node. Do not call this from within the CanEnterTransition
function or you will trigger an infinite loop.
Switch to a State¶
From an active state you can SwitchToLinkedState() which will switch the active state to the next one.
Alternatively you can manually call SetActive() on states to trigger their active state.
The SMInstance can also switch to any arbitrary state by calling SwitchActiveStateByQualifiedName().