Can You Set a State Tree Dynamically on an Actor Spawn?

Hello! I have been investigating the use of State Trees to control AI in my project, but I am looking for a way to set an actors state tree from a variable when it spawns. After having a look, I’m not too sure how to go about this or if it’s currently possible.

I would have a number of different state trees I can assign for an actor and would really prefer not to have to make a new child of my actor for every different state that could be used in it. Does anyone know of a way to set a state tree from a variable at spawn?

In case anyone else has this issue I found a way to do it by changing the state tree component when spawning an actor.

I created an Enum to define the different state tree options, and added it as a variable to the BP that’ll be spawned. Then I set the Enum variable in the BP to be exposed on spawn.

The actor BP that gets spawned in needs to have a state tree component, but the values can be left as none.

From the Begin Play node in the actor BP, I added a switch node on the Enum I created. Then from each pin I used the “Add state tree component” node to set the state tree for that case. Selecting the node allows you to define the state tree the component should use.

Then set the state tree component that’s already in the BP to the return value from the “Add state Tree component” node.

When spawning the actor you’ll just have to set the enum and it should work.

I couldn’t have figured it out without this person’s post:

Here’s a video tutorial if more explanation is needed: