GetAllActorsOfClass(): Cannot convert from TArray<> to TArray<> &

[I am following the answers from here][1], but I still couldn’t pass in the TArray into UGameplayStatics::GetAllActorsOfClass().

This is how my code looks like:

#include "GetDebt.h"
#include "../Player/Machine/BaseFrame.h"
#include "FleeingObstacle.h"


AFleeingObstacle::AFleeingObstacle(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer){
	//TTransArray<AActor*> Array = this->GetWorld()->PersistentLevel->Actors;
}

void AFleeingObstacle::BeginPlay(){
	Super::BeginPlay();

	//ERROR CODE
	TArray<ABaseFrame*> BaseFrameArray;
	UGameplayStatics::GetAllActorsOfClass(this->GetWorld(), ABaseFrame::StaticClass(), BaseFrameArray);
	if (BaseFrameArray.Num() > 0){
		LOG("Number of ABaseFrame in the TArray: " + FString::FromInt(BaseFrameArray.Num()));
	}
}

void AFleeingObstacle::Tick(float Delta){
	Super::Tick(Delta);
}

void AFleeingObstacle::Move(){
	LOG("MOVING... AT THE SPEED OF SOUND.")
}

and the header file contains the following code:

#pragma once

#include "Obstacle/Obstacle.h"
#include "FleeingObstacle.generated.h"

/**
 * 
 */
UCLASS()
class GETDEBT_API AFleeingObstacle : public AObstacle
{
	GENERATED_BODY()
public:
	//PROPERTIES
	

	//FUNCTIONS
	AFleeingObstacle(const FObjectInitializer& ObjectInitializer);

	void BeginPlay() override;

	void Tick(float DeltaSeconds) override;

	void Move() override;
};

I am still having this error popping up (Attachment provided):

What am I doing wrong this time? I am so confused.

UGameplayStatics::GetAllActorsOfClass will only accept TArray so your TArray BaseFrameArray; must be TArray BaseFrameArray;

Only after this then you can cast to whatever class you want.

Thank you, but can you please rephrase your answer? I had to re-read your answer numerous times in order for me to understand that you meant:

TArray<AActor*> BaseFrameArray;

And not:

TArray<AActor> BaseFrameArray;

Nor:

TArray<[INSERT SOME SUBCLASS OF AACTOR]> BaseFrameArray;

Nor:

TArray<[SAME AS ABOVE] *> BaseFrameArray;

Nor:

TArray BaseFrameArray;

Thank you again. :smiley:

Hi asperatology.

The solucion is simple. UGameplayStatics::GetAllActorsOfClass isn’t a template class so you must pass it an array of AActors*

Example Code:

TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AMyActor::StaticClass(), FoundActors);

for (AMyActor* TActor: FoundActors)
{
	AMyActor* MyActor = Cast<AMyActor*>(TActor);

	if(MyActor != nullptr)
			// Do what ever you need with your MyActor
}
1 Like

Thanks. That sort of makes sense.

I also understant what i do can use GetAllActorsOfClass

maybe a bit late, but just in case:

it should be

 for (AActor* TActor: FoundActors)
     {
         AMyActor* MyActor = Cast<AMyActor>(TActor);
     
         if(MyActor != nullptr)
                 // Do what ever you need with your MyActor
     }
1 Like