Dynamically switching State Stree reference in component

Hey,
I’m working with State Trees and they are great - but unfortunately you can not change their references in the State Tree Component dynamically at the moment.

Thanks to another user here I’ve made a C++ solution for it.

If you’ve set up Visual Studio for Unreal Engine (I’m using 5.5 and VS 2022 17.13.6)


Important: All black censored texts are your project name.

Steps to follow:
At first, create a custom C++ class inherited from the default-engine State Tree Component.

Open your [ProjectName].Build.cs file and add the marked line to the file. This way you include the StateTree and BrainComponent for using in C++.

Next open your header file MyStateTreeComponent.h. Include all the code below and make sure, you’ve entered your correct class name.

Open your C++ file MyStateTreeComponent.cpp and enter the code below. Make sure as well that you’ve entered your correct class name (here MyStateTreeComponent)

After building and compiling all C++ elements the class should appear in your C++ folder.
You can now add it as normal actor component to your blueprint classes, for example your player character.

Get the reference to the MyStateTreeComponent and search for StartStateTree. With this function you can now select a new StateTree reference and the tree logic will automatically start.


I hope this helps you guys who have/had the same problem as I :slight_smile:

1 Like