
Service
Service nodes attach to Composite or Task nodes and will execute if their branch is being executed. This means that as long as a node below the node is attached, it doesn't matter how many levels of parent-children are being executed—the Service will run as well. The following screenshot will help you visualize this:

This means that Service nodes are the eyes on the Behavior Tree's execution. In fact, they run continuously (if the sub-tree is active) and can perform checks and/or update Blackboard (see later) values in real-time.
Service nodes are really specific for your Behavior Tree application, so there are only two default ones. An example of their usage might be providing/updating information to the sub-tree. For instance, imagine a situation where a sub-tree (the enemy) is trying to kill the player. However, it would be dumb (well, it depends of the enemy type, trolls might not be so smart) to pursue this objective, even when the player isn't shooting back at the enemy. Thus, while the sub-tree is trying to kill the player, the sub-tree needs to find cover to reduce the damage the enemy takes. However, the enemy might be moving in the map, or the player might destroy the cover where our AI is hiding. Thus, the sub-tree needs information regarding the location of the nearest and safest cover, which is still in range of the player (an EQS Query can calculate that). A service can update this information in real-time so that when the sub-tree needs to use the data regarding the cover, they are ready. In this particular example, to find cover, running an Environment Query on the Service is a dynamic way to handle the task (we will look at this topic in Chapter 4, Environmental Querying System). Otherwise, the Service might check certain specified points in the map that have been placed by a designer and evaluate which one is the best for its given action.
As you can see, Service nodes can be really powerful, but they are also specific to the application you are using them for. Thus, they really depend on the AIs you are programming for your game.
The following screenshot shows a couple of examples of Services. Please notice that Services can be used along with Decorators, and that a Composite node can have more than one Service:

The only two available default Services (since you will need to program your own for your game, which we will do in Chapter 6, Extending Behavior Trees) are shown in the following screenshot:

- Set Default Focus: When this node becomes active, it automatically sets the Default Focus for the AI controller.
- Run EQS (Query on a regular basis): As the name suggests, it runs an Environmental Query (check out Chapter 4, Environment Querying System, for more information) on a regular basis to check for specific locations or actors. This was the kind of service that we needed in our example of finding cover for the enemy.
Now, we have learned about the different kind of nodes that compose a Behavior Tree. Now, it is time to explore Blackboards!