Hands-On Artificial Intelligence with Unreal Engine
上QQ阅读APP看书,第一时间看更新

Tasks

When we think of a tree, we often picture a large trunk with branches, and on those branches are leaves. In the context of UE4, those “leaves” are what we call “Tasks”. These are nodes that perform various actions, such as moving an AI, and can have Decorator or Service nodes attached to them. However, they do not have an output, which means that they do not play a role in the decision-making process itself, which is left entirely to Composite nodes. Instead, they define what an AI should do if that task needs to be executed.

Please note that Tasks can be as complex as you like. They can be as simple as waiting an amount of time, to being as complex as solving a puzzle while shooting at the player. Huge tasks are hard to debug and maintain, while small tasks can make the Behavior Tree easily overcrowded and huge. As a good AI Designer, you should try to find a balance between the size of the task and write them in such a way that they can be reused in different parts of the tree (or even in other trees).

A Task can either Fail (report Failure) or Succeed (report Success), and it will not stop its execution until one of these two results is reported. Composite nodes are responsible for taking care of this result and deciding on what to do next. Thus, a Task might need several frames to executed, but it will end only when it reports either Failure or Success. Keep this in mind when you move on to Chapter 6, Extending Behavior Trees, where you will create your own Tasks.

Tasks can have parameters (which you will be able to set in the Details Panel once a Task has been selected), and usually, they are either hard-coded values or Blackboard Key references (more on Blackboards later in this chapter).

Within the Behavior Tree editor, a Task appears as a purple box. In the following screenshot, you can see some examples of Tasks and how they look within the Editor:

Unreal comes with some built-in Tasks that are ready to be used. They are general and cover the basic cases that you will probably need. Obviously, they cannot be specific to your game, so you will need to create your own Tasks (we will look at this in Chapter 6, Extending Behavior Trees).

Here is the list of the built-in tasks in Unreal:

  • Finish with Result: Forces the Task to return a Finish Result (either Fail or Succeed) for this task immediately.
  • Make Noise: Produces a noise stimulus, which is used by the Perception System (this will be explored in Chapter 5, Agent Awareness).
  • Move Directly Toward: Like the following node, but it disregards the Navigation System.
  • Move To: Moves the Pawn (by using the Navigation System, which we will explore in Chapter 3, Navigation) to a location that's specified from the Blackboard (we will explore Blackboards later in this chapter).
  • Play Animation: As the name suggests, this node plays an animation. However, exceptions aside (and this is the reason why this node exists), it is good practice to separate animation logic and behavior logic. Therefore, try not to use this node, and instead improve your Animation Blueprint.
  • Play Sound: As the name suggests, this node plays a sound.
  • Push Pawn Action: Performs a Pawn Action (unfortunately, we will not cover them in this book).
  • Rotate to face BBEntry: Rotates the AI pawn to face a specific key that's been memorized inside the Blackboard (we will look at what Blackboards are later in this chapter).
  • Run Behavior: Runs another Behavior Tree as a whole sub-tree. As a result, it is possible to nest Behavior Trees to create and compose very complex behaviors.
  • Run Behavior Dynamic: Like the previous node, but it is possible to change which (sub-)Behavior Tree to execute at runtime.
  • Run EQSQuery: Performs an EQS Query (we will see what they are in Chapter 4, Environment Querying System) and stores the result within the Blackboard.
  • Set Tag Cooldown: Sets the timer (by using a tag) for a specific Cooldown node (which is a decorator that we will look at later in this chapter).
  • Wait: Stops the behavior for a specific amount of time. Arandom deviation can be specified to make the amount of time to wait different each time.
  • Wait Blackboard Time: Like the previous node, but the amount of time is retrieved from the Blackboard (more on Blackboards later in this chapter).

Now that we have looked at how a Task node works, let's explore Composite nodes, which make decisions based up whether a Task returns Failure or Success.