GetAllActorsOfClass not filling OutActor Array

Hello all,

So I’m working on a very simple AiController to setup some AI patrolling, and am running into a problem. I have both the subclass and TArray of Actor class declared in the header, mainly so that I can use the out array populated in the 3rd Parameter of the GetAllActorsOfClass method in another method to cycle through it to find the patrol locations. Below is my header and cpp:

Header:
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "AIController.h"
#include "FPSGuardController.generated.h"

class AFPSAIGuard;
class ATargetPoint;

UCLASS()
class FPSGAME_API AFPSGuardController : public AAIController
{
	GENERATED_BODY()

protected:
	AFPSAIGuard* Guard;

	UPROPERTY(EditDefaultsOnly, Category = "AI")
	TSubclassOf<AActor> TargetClass;

	// Create empty array for getter function to allocate returns into
	TArray<AActor*> PatrolLocations;

public:

	virtual void BeginPlay() override;

	UFUNCTION()
	void Patrol();
};

cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "FPSGuardController.h"
#include "FPSAIGuard.h"
#include "Kismet/GameplayStatics.h"


void AFPSGuardController::Patrol()
{
	if (PatrolLocations.Num() > 0)
	{
		for (int32 i = 0; i < PatrolLocations.Num(); i++)
		{
			MoveToActor(PatrolLocations[i]);
		}
		Patrol();
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Array did not populate!!!"));
	}
}

void AFPSGuardController::BeginPlay()
{
	Guard = Cast<AFPSAIGuard>(GetCharacter());
	if (Guard->GetCanPatrol())
	{
		UGameplayStatics::GetAllActorsOfClass(GetWorld(), TargetClass, PatrolLocations);
		
	}
}

Only thing I can think of is that for w/e reason I cannot use the populated array in another method.

EDIT:

Sorry forgot to mention that when I run this in the editor, I keep getting the log message I setup in the Patrol method which is called if the PatrolLocations array is empty.

What’s the problem exactly? Is it throwing an error or silently failing to find the patrol points?

Sorry forgot to add this to the main post. I have two actors in the level, that I fill in to the TargetClass parameter in the editor. So the out array (PatrolLocations) should have two indexes. One for each of the two TargetClass actors I have in the level that represent the patrol path. What’s happening is I’m getting the log message that fires when the PatrolLocations array is NOT greater than zero (ie empty).

Where is Patrol() being called from? I wonder if it is hapoening before the patrolpoint actors have been constructed.

On line 29 of the cpp file try “TargetClass::StaticClass()”