Add AI Task for Getting the Player Location or an Area Around Them

This commit is contained in:
Philip W 2023-09-28 21:29:04 +01:00
parent 44bf116340
commit 22e615d7cf
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,39 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "BTTask_FindPlayerLocation.h"
#include "NavigationSystem.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "GameFramework/Character.h"
UBTTask_FindPlayerLocation::UBTTask_FindPlayerLocation(FObjectInitializer const& ObjectInitializer)
{
NodeName = TEXT("Find Player Location");
}
EBTNodeResult::Type UBTTask_FindPlayerLocation::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
if (const ACharacter* const Player = Cast<ACharacter>(GetWorld()->GetFirstPlayerController()->GetPawn()))
{
if (UBlackboardComponent* const Blackboard = OwnerComp.GetBlackboardComponent())
{
if (SearchRandom)
{
FVector const Origin = Player->GetActorLocation();
const UNavigationSystemV1* const NavSystem = UNavigationSystemV1::GetCurrent(GetWorld());
if (FNavLocation RandomLocation; NavSystem->GetRandomPointInNavigableRadius(Origin, SearchRadius, RandomLocation))
{
Blackboard->SetValueAsVector(GetSelectedBlackboardKey(), RandomLocation.Location);
}
}
else
{
Blackboard->SetValueAsVector(GetSelectedBlackboardKey(), Player->GetActorLocation());
}
FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
return EBTNodeResult::Succeeded;
}
}
return EBTNodeResult::Failed;
}

View File

@ -0,0 +1,27 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/Tasks/BTTask_BlackboardBase.h"
#include "BTTask_FindPlayerLocation.generated.h"
/**
*
*/
UCLASS()
class ENDLESSVENDETTA_API UBTTask_FindPlayerLocation : public UBTTask_BlackboardBase
{
GENERATED_BODY()
public:
explicit UBTTask_FindPlayerLocation(FObjectInitializer const& ObjectInitializer);
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true"))
float SearchRadius = 150.f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true"))
bool SearchRandom = false;
};