forked from orfeasel/UE4-Cpp-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Fill out your copyright notice in the Description page of Project Settings. | ||
|
||
#include "EqsTut.h" | ||
#include "FindEnemyQueryContext.h" | ||
#include "EnvironmentQuery/EnvQueryTypes.h" | ||
#include "EnvironmentQuery/Items/EnvQueryItemType_Actor.h" | ||
#include "EqsTutCharacter.h" | ||
#include "MyAIController.h" | ||
|
||
void UFindEnemyQueryContext::ProvideContext(FEnvQueryInstance& QueryInstance, FEnvQueryContextData& ContextData) const | ||
{ | ||
Super::ProvideContext(QueryInstance, ContextData); | ||
|
||
//Get the Owner of this Query and cast it to an actor | ||
//Then, get the actor's controller and cast to it our AIController | ||
//This code works for our case but avoid that many casts and one-liners in cpp. | ||
AMyAIController* AICon = Cast<AMyAIController>((Cast<AActor>((QueryInstance.Owner).Get())->GetInstigatorController())); | ||
|
||
if (AICon && AICon->GetSeeingPawn()) | ||
{ | ||
//Set the context SeeingPawn to the provided context data | ||
UEnvQueryItemType_Actor::SetContextHelper(ContextData, AICon->GetSeeingPawn()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Fill out your copyright notice in the Description page of Project Settings. | ||
|
||
#pragma once | ||
|
||
#include "EnvironmentQuery/EnvQueryContext.h" | ||
#include "FindEnemyQueryContext.generated.h" | ||
|
||
/** | ||
* | ||
*/ | ||
UCLASS() | ||
class EQSTUT_API UFindEnemyQueryContext : public UEnvQueryContext | ||
{ | ||
GENERATED_BODY() | ||
|
||
virtual void ProvideContext(FEnvQueryInstance& QueryInstance, FEnvQueryContextData& ContextData) const override; | ||
|
||
|
||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Fill out your copyright notice in the Description page of Project Settings. | ||
|
||
#pragma once | ||
|
||
#include "AIController.h" | ||
#include "Perception/AIPerceptionComponent.h" | ||
#include "Perception/AISenseConfig_Sight.h" | ||
#include "MyAIController.generated.h" | ||
|
||
/** | ||
* | ||
*/ | ||
UCLASS() | ||
class EQSTUT_API AMyAIController : public AAIController | ||
{ | ||
GENERATED_BODY() | ||
|
||
private: | ||
|
||
/** BlackboardComponent - used to initialize blackboard values and set/get values from a blackboard asset */ | ||
UBlackboardComponent* BlackboardComp; | ||
|
||
/** BehaviorTreeComponent - used to start a behavior tree */ | ||
UBehaviorTreeComponent* BehaviorTreeComp; | ||
|
||
/** Blackboard Key Value Name */ | ||
const FName BlackboardEnemyKey = FName("Enemy"); | ||
|
||
/** The function that fires when the perception of our AI gets updated */ | ||
UFUNCTION() | ||
void OnPerceptionUpdated(TArray<AActor*> UpdatedActors); | ||
|
||
/** A Sight Sense config for our AI */ | ||
UAISenseConfig_Sight* Sight; | ||
|
||
protected: | ||
|
||
/** The Behavior Tree that contains the logic of our AI */ | ||
UPROPERTY(EditAnywhere) | ||
UBehaviorTree* BehaviorTree; | ||
|
||
/** The Perception Component of our AI */ | ||
UPROPERTY(VisibleAnywhere) | ||
UAIPerceptionComponent* AIPerceptionComponent; | ||
|
||
public: | ||
|
||
AMyAIController(); | ||
|
||
virtual void Possess(APawn* InPawn) override; | ||
|
||
/** Returns the seeing pawn. Returns null, if our AI has no target */ | ||
AActor* GetSeeingPawn(); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Read the full tutorial here: http://wp.me/p6hvtS-hy |