11. Make a Service

Services attach to Composite nodes, and will execute at their defined frequency as long as their branch is being executed. These are often used to make checks and to update the Blackboard. These take the place of traditional Parallel nodes in other Behavior Tree systems

In this case, the service is going to look for any Pawn that is not an AI Character, then set the TargetToFollow and TargetLocation Blackboard keys.

Steps

  1. Open the FollowerBT Behavior Tree by Double-clicking it in the Content Browser.

  2. Click New Service button.

    It will create in the same folder as the Behavior Tree.

  3. Rename it in the Content Browser to "AgroCheck".

  4. The editor should have already opened it for editing, but if not, Double-click it in the Content Browser.

  5. Create the following graph:

    Copy Example

    Click the image to expand it, or click here to open it in the window.

    variables.png

    The variable names, types, and values for this Blueprint:

    Variable Name Type Default Value
    DesiredObjectTypes Array of type EObjectTypeQuery One element set to Pawn
    MyLocation Vector 3 0,0,0
    AI_CON_Ref Follower_AI_CON_C None
    TargetToFollow BlackboardKeySelector None - However, this variable has its "Editable" flag set to True so you can change it, per instance, on the Behavior Tree.
    TargetLocation BlackboardKeySelector None - However, this variable has its "Editable" flag set to True so you can change it, per instance, on the Behavior Tree.

The variable DesiredObjectTypes is an Array of the type EObjectTypeQuery with a single element set to Pawn. To do this, select the DesiredObjectTypes variable in the My Blueprint panel, then in the Details Panel, under the Default Value category, click the plus_button.png button to add an element. Using the pull down on that element, select Pawn.

If the Default Values category doesn't show anything, Compile the Blueprint.

Here is what is going on in this graph:

  1. Event Tick works just like the same even in Actors, except it is ticked at a rate defined in the Behavior Tree.

  2. The Owner Actor passed in is always the Controller calling the Behavior Tree, so let us store that. But, only do that if we don't already have it stored, just to reduce some overhead from the cast node.

  3. Do a Multi Sphere Trace for Objects based on the location of the controlled pawn, and get all the pawns, minus the controlled pawn.

  4. The For Each loop then figures out if the player was hit.

  5. If it is, do a visibility trace to see if our AI pawn can see the player pawn.

  6. If yes, set the appropriate values on the blackboard object for the AI pawn (Target to Follow and Target Location).

  7. If no, clear the Target to Follow key on the blackboard.