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.
-
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.
-
CheckIsFinishedTask
-
Your behavior tree implementation
-
MarkFinishedTask
Both CheckIsFinishedTask and MarkFinishedTask are derived from GrailBaseTask which makes it possible to communicate the behavior tree’s DefaultBTBlackboard with Entity’s Blackboard.
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.