Is there a way to make GetAllActorsOfClass return an array of a class instead of AActors?

I’m trying to use GetAllActorsOfClass() to get all the actors of a “parent piece” class. This class has a variable named “coordinates”, which I want to compare with another variable within the class I’m calling from.
The problem is that I can only access the coordinates variable from a pointer of the parent piece class, but the GetAllActorsOfClass is only returning arrays of actors. The below code is what I have, and it returns an error saying that GetAllActorsOfClass needs 3 parameters.

TArray<AParent_Piece*> PiecesArray = UGameplayStatics::GetAllActorsOfClass(this, AParent_Piece::StaticClass());

for (int32 PiecesIndex = 0; PiecesIndex < PiecesArray.Num(); PiecesIndex++)
{
	if (PiecesArray[PiecesIndex].Coordinates = TheseCoordinates)
	{
	}
}

When I add a change it to this…

TArray<AActor*> PiecesArray;

UGameplayStatics::GetAllActorsOfClass(this, AParent_Piece::StaticClass(), PiecesArray);

for (int32 PiecesIndex = 0; PiecesIndex < PiecesArray.Num(); PiecesIndex++)
{
	if (PiecesArray[PiecesIndex].Coordinates = TheseCoordinates)
	{
	}
}

I receive a new error saying that the class “AActor” doesn’t have a “coordinates” variable. If I replace the “AActor*” with “AParent_Piece*”, it says that GetAllActorsOfClass can’t take an array that isn’t of AActors.

Does anyone know a workaround for this? With blueprint scripting, it’s really easy. But I want to figure this out in C++.

Thanks in advance!

Yep, that’s how it works. The blueprint version returns an array of your class, even though I can’t see any casting in the GetAllActorsOfClass() implementation.

I just cast them all manually to the class I need:

TArray<AMyClass*> MyActors;
TArray<AActor*> OutActors;

UGameplayStatics::GetAllActorsOfClass(GetWorld(), AMyClass::StaticClass(), OutActors);
for (AActor* a : OutActors)
{
    MyActors.Add(Cast<AMyClass>(a));
}

and then just use this MyActors array.

1 Like

that worked perfectly, thanks!