Tutorial: Your First 60 Minutes with StateTree

This is really great seeing a guide on this stuff. I spent a couple weeks digging through the source and doing experiments and wrote a guide for myself. I’d like to add to or clarify some points from the guide.

Under StateTree Execution Flow / StateTree State Selection

  1. When changing states, the tasks and states are exited in order from leaf to root of the previous state, and the enter state events happen from root to leaf.

From my experience this should be:
When changing states, the tasks and states are exited in order from the leaf of the current state branch until the common ancestor of the next state’s branch, and from the common ancestor states are entered through the next branch to reach the next state.

So if you’re current state is Root.Colors.Green and you go to Root.Colors.Red, only Green is exited before entering Red.

Also to clarify the point on StateTree Transition Selection. I like to think that Tasks can Finish or not Finish, and Tasks finishing trigger a State’s Completion (Succeed or Failed).

When a State enters a Completed state, it triggers a check on the Transitions.

If a State has no Transitions it will jump back up the StateTree passing on the Completed status to the ancestors. This could go all the way back up to the Root State which will force reentry of the whole StateTree! In other words, if you Complete a State without Transitions, you’ll loop over the StateTree re-entering States which may not be what you intended! To interrupt this, you can add a Transition to the Parent of a Child State like On State Completed transition to the Parent State.

Edit: Another option to prevent re-entry is, in your tasks, set bShouldStateChangeOnReselect to false. In Blueprints, under Class Defaults it’s by default true which is useful for restarting a sequence of animations, but if you want to avoid re-entry, you want this false. In C++ you’d subclass StateTreeTaskBlueprintBase and set bShouldStateChangeOnReselect to false, and create your BP Tasks from it.

Also it needs to be mentioned that every State on the active branch gets ticked every frame to run Tasks and check for Transitions.

5 Likes