Unreal Engine AI C++ PART -1 Detect Target
In Game Development Artificial Intelligence is Mostly used for Making Game Play is More Funny and nice connected with game player;
NPC : Non-Player Character (AI);
UE4 is well Supported. We can do In this section with C++ with more great power;
Now We Use Simple AI Game Play Program.
Where We use AI : Like - Enemy, Supported Player Character's, making Some Auto Cars in Game etc. we use it every where we can do but we need look on your game performance. Make a AI when you needed;
We Can Add a Smart AI?
If We looking generally a Game have 20 to 50 levels or 5 hours to 50 hours a game take time to full game play; We add Smart AI what happens Player is dead again or failed in mission's and Game Player (Gamer) is not like this games because they want complete missions with easy and funny game play; Or Some Missions we add powerful AI but not SMART AI;
SMART AI : like When Player fire the hide on cover or they know where is from player comes, they have more power compare to player in my words this simply means AI have more powers to kills Player;
We can use First-Person Template or Third-Person Template your desire depends on You.
Setup Requirements: UE4 4.23, C++ Template First-Person Player, Visual Studio 2019
Start : First Make a Project First Person Character in C++
Go to Modes |Volumes | Nav Mesh Bounds Volume
Nav Mesh : Area of AI-Controller move into or across.
drag it on level or View-Port use Scale to increase size; press P to see nev Mesh

Creating A Black Board : Go to Content | Right Click or Add New | Artificial Intelligence | Black Board ;
Black Board : It contains variables like > Vector, object, bool etc and Behavior Tree use it; The Data use only for decision making ; we Call it Brain of AI because it takes Keys or Black Board Keys;
Open Up Black Board BP Click On New Key | Object | "Target" save asset
Our Key is Set - "Target"
Creating Behavior Tree : Go to Content | Right Click or Add New | Artificial Intelligence | Behavior Tree
Behavior Tree : Processing the given variables in Black Board; Make Decision Making for AI and AI working when game Is running;
Check Out The Black Board is Setup currently;
Behavior Tree Connect With AI Character or Enemy
Behavior Tree chooses a behavior to be exhibited by an AI-Controller ;
AI-Controller : Simply named Controller of AI
C++ IS
First Open Project in Visual Studio 2019
Go To Solutions Explorer | [projectName].Build.cs|Open it and Add Code
PublicDependencyModuleNames.AddRange(new string[] {"AIModule","GameplayTasks" });
Compile
Now Go to Add New | New C++ Class | check All Classes | search AIController | NEXT | name it finish ;
EnemyAIController.h
class MASTERCHARATER_API AEnemyAIController : public AAIController
{
GENERATED_BODY()
public:
AEnemyAIController();
class UBehaviorTreeComponent* BehaviourTreeComp;
UBlackboardComponent* BlackBoardComp;
virtual void OnPossess(APawn* InPawn) override;
FBlackboard::FKey TargetKeyId;
};
EnemyAIController.cpp
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BlackboardComponent.h"
AEnemyAIController::AEnemyAIController() {
BehaviourTreeComp = CreateDefaultSubobject<UBehaviorTreeComponent>("Behaviour Tree");
BlackBoardComp = CreateDefaultSubobject<UBlackboardComponent>("Black Board ");
}
void AEnemyAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
}
Now Create Character (Enemy) Class Add New | C++ Class | Character | name it | Finish
AIEnemy .h
UCLASS()
class MASTERCHARATER_API AAIEnemy : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AAIEnemy();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, Category = "Behaviour")
class UBehaviorTree* EnemyBehaviourTree;
};
Now Again Open EnemyAIController.cpp
void AEnemyAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
AAIEnemy* Charactear = Cast<AAIEnemy>(InPawn);
if (Charactear && Charactear->EnemyBehaviourTree)
{
// Set Black Board;
BlackBoardComp->InitializeBlackboard(*Charactear->EnemyBehaviourTree->BlackboardAsset);
// Set Up Key We Create in BlackBoard Blueprint;
TargetKeyId = BlackBoardComp->GetKeyID("Target");
//Check Out The Behaviour tree is Now Set;
BehaviourTreeComp->StartTree(*Charactear->EnemyBehaviourTree);
}
}
Save Compile
EnemyAIController and AIEnemy class make both Blueprint classes;
Go to AIEnemy_BP (Blueprint) set Mesh(Recommended) and Animation(if you have) or you can Choose First Player Character Mesh and Animation only you need to Change material;

Now Go To Components | AIEnemy_BP(Self) | Select Our AI Controller to EnemyAIController_BP;

Check Out Our Behavior Tree we made;
Behavior Tree Connected with AI Controller which turn is Connected with to a Character;
Behavior Tree
Six Different Types of Node
- Task : purple Node. This contains Blueprint code to run. Task return true or false depending on succeeded or not;
- Decoder : Boolean Condition for execution of a Node. It Check Conditions it used within Selector or Sequence Node;
- Service : Run blueprint code when it Ticks; it tick slower than per frame tick ; Used for query the scene updates;
- Selector : This runs Sub-Trees from Left to Right; True execution goes backup the tree;
- Sequence: Left to Right until it encounter a failure.
- Simple Parallel : Single Task (Purple) in Parallel with a sub-tree(gray)
BTService
Service to attach with nodes in Behavior Tree and will execute at their defined frequency;
Add New C++ | Check All Classes | Search |BTService | Next | Name it Finish;

BTService_FindPlayer.h
#include "CoreMinimal.h"
#include "BehaviorTree/BTService.h"
#include "BTService_FindPlayer.generated.h"
/**
*
*/
UCLASS()
class MASTERCHARATER_API UBTService_FindPlayer : public UBTService
{
GENERATED_BODY()
public:
UBTService_FindPlayer();
virtual void TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds) override;
};
BTService_FindPlayer.cpp
#include "BTService_FindPlayer.h"
#include "AI_Enemy/EnemyAIController.h"
#include "Engine/Engine.h"
#include "GameFramework/PlayerController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/Blackboard/BlackboardKeyType_Object.h"
UBTService_FindPlayer::UBTService_FindPlayer() {
bCreateNodeInstance = true;
}
void UBTService_FindPlayer::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
AEnemyAIController* AEController = Cast<AEnemyAIController>(OwnerComp.GetAIOwner());
if (AEController)
{
auto PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
OwnerComp.GetBlackboardComponent()->SetValue<UBlackboardKeyType_Object>(AEController->TargetKeyId,PlayerPawn);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Player Find What Next??"));
}
}
Save And Compile!
Go To Behavior Tree Drag Down ROOT Node -> Selector -> Right Click on it > Add Service > Find Player
Drag Our AIEnemy_BP Character into Scene or View-port run game;
Comments
Post a Comment