Advertisement
orfeasel

Next Target Selection Task

Dec 31st, 2015
20,975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. EBTNodeResult::Type UBTTargetPointSelection::ExecuteTask(UBehaviorTreeComponent & OwnerComp, uint8 * NodeMemory)
  2. {
  3.     AMyAIController* AICon = Cast<AMyAIController>(OwnerComp.GetAIOwner());
  4.  
  5.     /*If the Controller is valid:
  6.     1)Get the Blackboard Component and the Current Point of the bot
  7.     2)Search for the next point, which will be different from the Current Point*/
  8.     if (AICon)
  9.     {
  10.        
  11.         UBlackboardComponent* BlackboardComp = AICon->GetBlackboardComp();
  12.         ABotTargetPoint* CurrentPoint = Cast<ABotTargetPoint>(BlackboardComp->GetValueAsObject("LocationToGo"));
  13.  
  14.         TArray<AActor*> AvailableTargetPoints = AICon->GetAvailableTargetPoints();
  15.        
  16.         //This variable will contain a random index in order to determine the next possible point
  17.         int32 RandomIndex;
  18.  
  19.         //Here, we store the possible next target point
  20.         ABotTargetPoint* NextTargetPoint = nullptr;
  21.  
  22.         //Find a next point which is different from the current one
  23.         do
  24.         {
  25.             RandomIndex = FMath::RandRange(0, AvailableTargetPoints.Num()-1);
  26.             //Remember that the Array provided by the Controller function contains AActor* objects so we need to cast.
  27.             NextTargetPoint = Cast<ABotTargetPoint>(AvailableTargetPoints[RandomIndex]);
  28.         } while (CurrentPoint == NextTargetPoint);
  29.  
  30.         //Update the next location in the Blackboard so the bot can move to the next Blackboard value
  31.         BlackboardComp->SetValueAsObject("LocationToGo", NextTargetPoint);
  32.  
  33.         //At this point, the task has been successfully completed
  34.         return EBTNodeResult::Succeeded;
  35.     }
  36.     return EBTNodeResult::Failed;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement