Add AI Task for Random Patrolling of a Set Area

This commit is contained in:
Philip W 2023-09-28 21:26:45 +01:00
parent a65978a915
commit 44bf116340
2 changed files with 56 additions and 0 deletions

View File

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

View File

@ -0,0 +1,24 @@
// 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_FindRandomLocation.generated.h"
/**
*
*/
UCLASS()
class ENDLESSVENDETTA_API UBTTask_FindRandomLocation : public UBTTask_BlackboardBase
{
GENERATED_BODY()
public:
explicit UBTTask_FindRandomLocation(FObjectInitializer const& ObjectInitializer);
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI", meta = (AllowPrivateAccess = "true"))
float SearchRadius = 1000.f;
};