Behavior Tree Integration

Thanks to Grail’s modularity and flexibility Unreal Engine’s plugin provides users with a mean to use UE’s behavior trees within Grail.

Behavior Tree Behavior

BaseBTBehavior class is a Behavior wrapper over Unreal Engine’s behavior tree.

BaseBTBehavior requires some particular setup:
  • Make sure that AI Entity’s private Grail Blackboard is provided with a pointer to AAIController which will be executing the behavior tree. This pointer should reside on the Blackboard under EntityBlackboardKeys::AI_CONTROLLER key, which can be found in the "BlackboardKeys/EntityBlackboardKeys.h" file.

  • Make sure that behavior tree to be executed is provided with Unreal Engine’s blackboard inheriting from DefaultBTBlackboard found in the plugin (Plugins/Grail/Content/Blackboards).

  • Pass the pointer to the behavior tree to the BaseBTBehavior constructor.

Never-ending Tree Execution

If you do everything in the above steps and assign the Behavior to the AI Entity, the behavior tree will execute in a loop, just as it usually works in Unreal Engine. It will end its execution only when the Behavior is interrupted (with another or with nullptr). Keep in mind that if you made the Behavior uninterruptible by passing a proper argument to the constructor, it cannot be interrupted, therefore it will be executed in an infinite loop.

One-Time Tree Execution

You may want to execute the behavior tree exactly once and finish the Behavior when it’s done. To achieve that you’d have to construct a tree in a particular way using some tasks provided in Grail’s plugin.

At the root of the tree there should be a Sequence node with those nodes connected to it:
  1. CheckIsFinishedTask

  2. Your behavior tree implementation

  3. MarkFinishedTask

Both CheckIsFinishedTask and MarkFinishedTask are derived from GrailBaseTask which makes it possible to communicate the behavior tree’s DefaultBTBlackboard with Entity’s Blackboard.

One-time execution Behavior Tree example:

Tree Example

You can modify this approach to make it possible to execute the behavior tree until a custom condition is met i.e. execute the tree fifteen times or execute it until the executing character has allies in its vicinity.

Behavior Tree Selected by Utility

To make the BaseBTBehavior usable with Utility AI all you need to do is to create an instance of UBaseBTBehaviorProvider, configure it with a name, interruptibility and behavior tree. Keep in mind that the name provided to UBaseBTBehaviorProvider has to be identical to the one used in the configuration file for Grail to be able to pair the two. Then add it to the BehaviorLoader. More details on the Utility AI setup here.

Behavior Provider for behavior trees example:

BehaviorProvider Example

Customization

Classes BaseBTBehavior and UBaseBTBehaviorProvider can be inherited from by the user for customization purposes. If what is provided in the plugin is not enough for you, it is certainly possible to adjust it to your needs.