Overview
StateTree is a general-purpose hierarchical state machine that combines the Selectors from behavior trees with States and Transitions from state machines. Users can create highly performant logic that stays flexible and organized.
You can learn more about State Trees by reading the State Tree Overview documentation.
Goals
In this guide you will use a State Tree to create a moving target the player can shoot and destroy.
Objectives
Create a Blueprint for a target Actor the player can shoot and destroy.
Create the State Tree that contains the logic to move and destroy the target actor.
Create State Tree Tasks and use them in the State Tree.
Create a State Tree Evaluator and use it in the State Tree.
1 - Required Setup
Create a new project then select the Games category and the First Person template. Enter your project's location and name. Click Create.
Click image for full size.
Click Settings > Plugins to open the Plugins window.
Search for and enable the GameplayStateTree and StateTree plugins. Restart the Unreal Engine editor.
Section Results
In this section, you created a new project and enabled the State Tree plugins. You are now ready to create the State Tree you will use in the target Actor.
2 - Create the State Tree
In this section, you will create a State Tree that will be used by the target Actor. This State Tree is intended to be used as a component of the target Actor, so you will use the State Tree Component Schema for this example.
In the Content Browser, right click and select Artificial Intelligence > StateTree.
Select the StateTreeComponentSchema class and click Select.
Name the State Tree ST_ShootingTarget.
Section Results
In this section, you created a State Tree with the Component Schema that will be used by a target Actor.
3 - Create the Shooting Target Blueprint
In this section, you will create the Blueprint for the target Actor that will move and receive damage from the player.
In the Content Browser, right click and select Blueprint Class from the Create Basic Asset section.
In the Pick Parent Class window, click the Actor button to create a new Actor Blueprint.
Name the Blueprint BP_ShootingTarget.
Open BP_ShootingTarget and go to the Components window. Click +Add and select Static Mesh.
Go to the Details panel and scroll down to the Static Mesh section.
Click the Static Mesh dropdown and select 1M_Cube.
Set the Scale of the Static Mesh to X = 0.2, Y = 2.0, Z = 2.0.
In the Components window, click +Add and select StateTree.
Go to the Details panel and scroll down to the AI section.
Click the State Tree dropdown and select ST_ShootingTarget.
If the State Tree does not appear in the dropdown, it means that the incorrect schema was selected. Make sure to select the StateTreeComponentSchema to use in the State Tree component.
Right-click the Static Mesh component and select Add Event > Add OnComponentHit.
Create a new variable and name it HitCount.
Go to the Details panel and set the Variable Type to Integer.
Drag the HitCount variable to the Event Graph and select Get HitCount.
Drag from the HitCount node and search for and select Increment Int.
Connect the On Component Hit node to the Increment Int node.
Section Results
In this section, you created the Shooting Actor Blueprint the player will shoot during gameplay. You are now ready to create the Idle and Dead States of the State Tree.
4 - Create the Idle and Dead States
Go back to ST_ShootingTarget and under the Schema section, click the Context Actor Class dropdown. Select BP_ShootingTarget.
Click + Add State to create a new State and name it Idle.
Go to the Details panel and scroll down to the Transitions section.
Expand the Go to State section and click the Transition To dropdown. Select Root. This will set the State Tree to transition back to the Root State once this State is completed.
Create another State and name it Dead.
Select the Idle State and add another Transition.
Click the Trigger dropdown and select On Tick.
Click the Transition To dropdown and select Dead.
Add a new Condition by clicking on the plus sign. Click the dropdown and select Integer Compare.
Expand the structure and click on the Left Bind dropdown and select Actor > Hit Count. This binds the value of Hit Count inside BP_ShootingTarget to the StateTree Condition.
Enter 5 as the value of the Right integer.
Section Results
In this section, you added the Idle and Dead States to the State Tree. You are now ready to create a State Tree Task that will handle the Dead State.
5 - Create a New State Tree Task
In this section, you will create a new State Tree Task that will destroy the Actor when the Dead State is executed.
In the Content Browser, right click and select Blueprint Class.
In the Pick Parent Class window, expand the All Classes dropdown and search for and select StateTreeTaskBlueprintBase.
Click Select to create the asset.
Name the Blueprint class STT_Destroy.
Open STT_Destroy by double-clicking it in the Content Browser. Go to the Functions section and click the Override dropdown. Select ExitState.
Create a new variable and name it Actor. Set its type to Actor Object Reference.
Go to the Details panel, and set its Category to Context.
Drag the Actor variable to the Event Graph and select Get Actor.
Drag from the Actor node and search for and select Destroy Actor.
Connect the Event ExitState node to the Destroy Actor node.
Section Results
In this section, you create a new State Tree Task that runs once the Dead State is executed. This Task destroys the Actor.
6 - Finish the Dead State
Go back to ST_ShootingTarget and select the Dead State. Add a new Task and select Debug Text Task from the dropdown.
Enter ‘Actor Destroyed' in the Text field.
Enter a Text Color and a Font Scale.
Add another Task and select Delay Task from the dropdown.
Enter 2.0 for the Duration.
Add a third Task and select STT_Destroy from the dropdown. This will destroy the Actor.
Create a new Transition and set the Trigger to On State Completed.
Set the Transition To dropdown to Tree Succeeded.
Drag BP_ShootingTarget to your Level and press Play. Shoot the Blueprint and confirm the State Tree is working.
Section Results
In this section, you completed the Dead State and tested that the Shooting Actor Blueprint can receive damage and be destroyed.
7 - Add a Spline Path
In this section, you will create an Actor with a Spline component. This spline will be used by the Shooting Target Blueprint to move in the Level.
In the Content Browser, right click and select Blueprint Class from the Create Basic Asset section.
In the Pick Parent Class window, click the Actor button to create a new Actor Blueprint.
Name the Blueprint BP_SplineActor.
Open BP_SplineActor and go to the Components window. Click +Add and select Spline.
Compile and Save the Blueprint.
Section Results
In this section, you created a generic Blueprint Actor that has a Spline component. This Spline will be used to create a moving path for the Shooting Actor in the Level.
8 - Add a State Tree Evaluator
Now you will create a State Tree Evaluator that will search for all Spline Actors in the Level and return the closest one to the State Tree.
In the Content Browser, right click and select Blueprint Class from the Create Basic Asset section.
In the Pick Parent Class window, expand the All Classes dropdown and search for and select StateTreeEvaluatorBlueprintBase.
Click Select to create the asset.
Name the Blueprint class STE_GetSpline.
Open STE_GetSpline and go to the Functions section of the My Blueprint panel.
Click the Override dropdown and select TreeStart.
Drag from the Event TreeStart node and search for and select Get All Actors of Class.
Click the Actor Class dropdown and select BP_SplineActor.
Create a new variable and name it Actor.
Go to the Details panel and set the Variable Type to Actor Object Reference.
Click the Category dropdown and enter ‘Context'.
Drag from the Out Actors pin of the Get All Actors from Class node and search for and select Find Nearest Actor.
Drag the Actor variable to the Event Graph and select Get Actor.
Drag from the Actor node and search for and select Get Actor Location.
Connect the Return Value of the Get Actor Location node to the Origin pin of the Find Nearest Actor node.
Drag from the Return Value pin of the Find Nearest Actor node and search for and select Cast to BP_SplineActor.
Right click on the As BP Spline Actor pin of the Cast to BP_SplineActor node and select Promote to Variable.
Name this variable to SplineActor and enter ‘Output' for its Category.
Compile and Save the Blueprint.
Go back to ST_ShootingTarget and under the StateTree window, click the plus sign next to Evaluators.
Click the dropdown and select STE_GetSpline.
Section Results
In this section, you created a State Tree Evaluator that will execute when the State Tree begins execution. This Evaluator checks for all Spline Actors in the Level and returns the closest one to the Shooting Actor.
9 - Add a State Tree Task to Move Along Spline
In this section, you will create a new State Tree Task that will move the Actor along the BP_SplineActor's spline.
In the Content Browser, right click and select Blueprint Class.
In the Pick Parent Class window, expand the All Classes dropdown and search for and select StateTreeTaskBlueprintBase.
Click Select to create the asset.
Name the Blueprint class STT_MoveAlongSpline.
Open STT_MoveAlongSpline and go to the Functions section of the My Blueprint panel. Click the Override dropdown and select Tick.
Create a new variable and name it Actor.
Go to the Details panel and set the Variable Type to Actor Object Reference.
Click the Category dropdown and enter ‘Context'.
Create a new variable and name it SplineActor.
Go to the Details panel and set the Variable Type to BP_SplineActor.
Enter ‘Input' as the Category.
Drag the Actor variable to the Event Graph and select Get Actor.
Drag from the Actor node and search for and select Set Actor Location.
Connect the Tick node to the Set Actor Location node.
Drag the SplineActor to the Event Graph and select Get Spline Actor.
Drag from the SplineActor node and search for and select Spline.
Drag from the Spline node and search for and select Get Location at Distance Along Spline.
Connect the Return Value of the Get Location at Distance Along Spline node and connect it to the New Location pin of the Set Actor Location node.
Right click the Distance pin of the Get Location at Distance Along Spline node and select Promote to Variable.
Drag from the Set Actor Location node and search for and select Branch.
Drag the Distance variable to the Event Graph and select Get Distance.
Drag from the Distance node and search for and select Less.
Connect the Less node to the Condition pin of the Branch node.
Drag the SplineActor to the Event Graph and select Get Spline Actor.
Drag from the SplineActor node and search for and select Spline.
Drag from the Spline node and search for and select Get Spline Length.
Connect the Return Value from the Get Spline Length node and connect it to the lower pin of the Less node.
Drag the Distance variable to the Event Graph and select Get Distance.
Drag from the Distance node and search for and select Add.
Create a new variable of type Float and name it MovementSpeed.
Connect MovementSpeed to the lower pin of the Add node.
Drag the Distance variable to the Event Graph and select Set Distance.
Connect the Add node to the Set Distance node.
Connect the True pin of the Branch node to the Set Distance node, as seen below.
Select the MovementSpeed variable and set its Default value to 5.0.
Drag the Distance variable to the Event Graph and select Set Distance.
Connect the False pin of the Branch node to the Set Distance node.
Connect both Set Distance nodes to the Return Node with a Return Value of Running.
Section Results
In this section, you create a State Tree Task that moves the Shooting Actor Blueprint along the spline path created with the Spline Actor Blueprint.
10 - Finalize the State Tree
Go back to ST_ShootingTarget and click +Add State. Name the new State MoveAlongSpline.
Click and drag the MoveAlongSpline State into the Idle State to parent it.
Go to the Details panel and add a new Task by clicking the + button.
Click the dropdown and select STT_MoveAlongSpline.
Click the Bind dropdown next to Spline Actor and select STE Get Spline > Spline Actor.
Drag BP_SplineActor to the Level.
Select the Spline component of BP_SplineActor. Alt-drag to create new spline points and create a closed shape.
Select the Spline component of the Spline Actor Blueprint and scroll down to the Spline section. Enable the Closed Loop checkbox to make the spline a closed shape.
Press Play and shoot at the target.