Skip to content

Commit

Permalink
Added EQS Tut
Browse files Browse the repository at this point in the history
  • Loading branch information
orfeasel committed Oct 13, 2016
1 parent 8635c91 commit c584b67
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
24 changes: 24 additions & 0 deletions EQS_Cpp/FindEnemyQueryContext.cpp
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());
}
}
19 changes: 19 additions & 0 deletions EQS_Cpp/FindEnemyQueryContext.h
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 added EQS_Cpp/FindHidingSpot.uasset
Binary file not shown.
76 changes: 76 additions & 0 deletions EQS_Cpp/MyAIController.cpp
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;
}
55 changes: 55 additions & 0 deletions EQS_Cpp/MyAIController.h
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();

};
1 change: 1 addition & 0 deletions EQS_Cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Read the full tutorial here: http://wp.me/p6hvtS-hy

0 comments on commit c584b67

Please sign in to comment.