forked from orfeasel/UE4-Cpp-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyAIController.cpp
76 lines (59 loc) · 2.34 KB
/
MyAIController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Fill out your copyright notice in the Description page of Project Settings.
#include "EqsTut.h"
#include "MyAIController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BehaviorTree.h"
#include "EqsTutCharacter.h"
void AMyAIController::OnPerceptionUpdated(TArray<AActor*> UpdatedActors)
{
//If our character exists inside the UpdatedActors array, register him
//to our blackboard component
for (AActor* Actor : UpdatedActors)
{
if (Actor->IsA<AEqsTutCharacter>() && !GetSeeingPawn())
{
BlackboardComp->SetValueAsObject(BlackboardEnemyKey, Actor);
return;
}
}
//The character doesn't exist in our updated actors - so make sure
//to delete any previous reference of him from the blackboard
BlackboardComp->SetValueAsObject(BlackboardEnemyKey, nullptr);
}
AMyAIController::AMyAIController()
{
//Components Init.
BehaviorTreeComp = CreateDefaultSubobject<UBehaviorTreeComponent>(FName("BehaviorComp"));
BlackboardComp = CreateDefaultSubobject<UBlackboardComponent>(FName("BlackboardComp"));
AIPerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(FName("PerceptionComp"));
//Create a Sight Sense
Sight = CreateDefaultSubobject<UAISenseConfig_Sight>(FName("Sight Config"));
Sight->SightRadius = 1000.f;
Sight->LoseSightRadius = 1500.f;
Sight->PeripheralVisionAngleDegrees = 130.f;
//Tell the sight sense to detect everything
Sight->DetectionByAffiliation.bDetectEnemies = true;
Sight->DetectionByAffiliation.bDetectFriendlies = true;
Sight->DetectionByAffiliation.bDetectNeutrals = true;
//Register the sight sense to our Perception Component
AIPerceptionComponent->ConfigureSense(*Sight);
}
void AMyAIController::Possess(APawn* InPawn)
{
Super::Possess(InPawn);
if (BehaviorTree)
{
//Initialize the Blackboard and start the attached behavior tree
BlackboardComp->InitializeBlackboard(*BehaviorTree->BlackboardAsset);
BehaviorTreeComp->StartTree(*BehaviorTree);
}
//Register the OnPerceptionUpdated function to fire whenever the AIPerception get's updated
AIPerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &AMyAIController::OnPerceptionUpdated);
}
AActor* AMyAIController::GetSeeingPawn()
{
//Return the seeing pawn
UObject* object = BlackboardComp->GetValueAsObject(BlackboardEnemyKey);
return object ? Cast<AActor>(object) : nullptr;
}