Advertisement
orfeasel

AI Controller Source File

Jan 14th, 2016
16,451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include "AICharacter.h"
  2. #include "BehaviorTree/BlackboardComponent.h"
  3. #include "BehaviorTree/BehaviorTreeComponent.h"
  4. #include "BehaviorTree/BehaviorTree.h"
  5.  
  6.  
  7. AMyAIController::AMyAIController()
  8. {
  9.     //Initialize the behavior tree and blackboard components
  10.     BehaviorComp = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorComp"));
  11.  
  12.     BlackboardComp = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackboardComp"));
  13. }
  14.  
  15. void AMyAIController::Possess(APawn* Pawn)
  16. {
  17.     Super::Possess(Pawn);
  18.  
  19.     //Get the possessed Pawn. If it's the AI Character we created
  20.     //initialize it's blackboard and start it's corresponding behavior tree
  21.     AAICharacter* AICharacter = Cast<AAICharacter>(Pawn);
  22.     if (AICharacter)
  23.     {
  24.         if (AICharacter->BehaviorTree->BlackboardAsset)
  25.         {
  26.             BlackboardComp->InitializeBlackboard(*(AICharacter->BehaviorTree->BlackboardAsset));
  27.             BehaviorComp->StartTree(*AICharacter->BehaviorTree);
  28.         }
  29.     }
  30. }
  31.  
  32. void AMyAIController::SetSeenTarget(APawn* Pawn)
  33. {
  34.     //Registers the Pawn that the AI has seen in the blackboard
  35.     if (BlackboardComp)
  36.     {
  37.         BlackboardComp->SetValueAsObject(BlackboardKey, Pawn);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement