Teaching our AI to walk with the Behavior Tree

We can now move on to the heart of the AI: the Behavior Tree. Return to Content Browser and open EnemyBehavior. On the right, change Blackboard Asset to EnemyBlackboard. You should now see our PatrolPoint Blackboard Key appear in the Blackboard panel on the bottom left.

Now, look at the Behavior Tree panel, which will look similar to the Event Graphs we are used to seeing inside of Blueprints. This is where we'll create the branching logic that will determine which Actions to perform, based on the conditions the enemy is currently experiencing. The top Level of the logic tree will always be the ROOT node, which simply serves to indicate where the logic flow will start from.

The darker line at the bottom of the Behavior Tree nodes is the connection point between nodes. You can click and drag a wire from the dark area at the bottom of the ROOT node and drop it onto empty grid space to get a new selection menu popup that will allow you to add additional nodes to the Behavior Tree. Do so now and select the Selector option, as shown in the following screenshot:

The two primary branching node types you'll utilize are Selector and Sequence. A Selector node runs each of the nodes connected underneath it—called its children—from left to right, but succeeds and stops running as soon as one child successfully runs. Thus, if a Selector node has three children, then the only way the third child node will be run is if the first two children failed to execute because the conditions attached to them were false. A Sequence node is just the opposite. It also runs all the children in a sequence from left to right, but the Sequence node only succeeds if all of the children succeed. The first child to fail causes the whole sequence to fail, ending the execution and aborting the sequence.

Underneath the Selector node, attach two Sequence nodes next to one another. Select the Sequence node on the left and change Node Name, in the Description panel, to Move to Patrol. Next, select the other Sequence node and change its
name to Idle.

Notice the faint grey circles with numbers inside of them that are positioned to the upper-right corner of the two Sequence nodes. These indicate the execution order of the nodes, which are ordered according to their left-to-right positions. The first node to be evaluated will be labeled with a 0 badge.

Now, we need to add Actions that will be triggered by the nodes. Drag a wire down from the Move to Patrol node and attach a Move To node. This node will be purple in color, visually distinguished as a node that results in Actions. These nodes are called task nodes and will always be the bottommost nodes in a Behavior Tree. As a consequence, you will notice there is no attachment point for additional nodes at the bottom of a task node, as shown in the following screenshot:

Looking at the Details panel of our newly added Move To task node, you will notice a few new options that will allow us to tweak the movement of our enemy character. The first of these is a field called Acceptable Radius. This field indicates the acceptable distance that the enemy can be away from the target to complete the task. We don't want the enemy to move sideways or strafe toward the target, so we can leave Allow Strafe unchecked. Blackboard Key determines the location that the Actor will be moved to. As we only have one Blackboard Key, this field should be automatically set to our intended target, which is PatrolPoint.

Next, drag a wire down from the node that we called Idle and attach it to a Wait task node. The Wait node contains only two configuration fields. The value of the Wait Time field determines how long the enemy will wait between movements to the next PatrolPoint. Set this value to 3.0 to add a 3-second pause between patrols. The field below, named Random Deviation, allows us to add randomness to the amount of time that passes while the enemy waits. Enter 1.0 into this field to add a 1-second variation to our 3-second wait time. This will result in a pause of random length between 2 and 4 seconds between patrols.

Save the Behavior Tree and then return to the FirstPersonExampleMap tab. Find the enemy we placed, called Enemy1, and select it in World Outliner. In the Details panel, navigate down to the Default category and set Patrol Point 1 to the PatrolPoint1 object, set Patrol Point 2 to the PatrolPoint2 object, and set Current Patrol Point to the patrol point object furthest away from the enemy's starting position. We want to set these patrol points on the enemy instance in the world, rather than as defaults on the variables within the enemy Blueprint, because we want each enemy that we create in the future to have its own set of patrol points. This is shown in the following screenshot:

Save and click Play to test. You should see the red enemy character start navigating to the first of the two patrol points. When it reaches the first point, it will briefly pause and then start walking to the second patrol point. This pattern will continue back and forth as long as the game is running.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset