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

**URL:** <https://forums.unrealengine.com/t/is-there-a-way-to-make-getallactorsofclass-return-an-array-of-a-class-instead-of-aactors/487445>\
**Category:** C++\
**Tags:** actor, unreal-engine, question, CPP, actor-class\
**Created:** [August 14, 2021, 10:35pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-make-getallactorsofclass-return-an-array-of-a-class-instead-of-aactors/487445 "2021-08-14T22:35:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samathan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/samathan/32/1058905_2.png) [@Samathan](https://forums.unrealengine.com/u/Samathan)\
**Post date:** [August 14, 2021, 10:35pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-make-getallactorsofclass-return-an-array-of-a-class-instead-of-aactors/487445/1 "2021-08-14T22:35:33Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [August 15, 2021, 9:17am UTC](https://forums.unrealengine.com/t/is-there-a-way-to-make-getallactorsofclass-return-an-array-of-a-class-instead-of-aactors/487445/2 "2021-08-15T09:17:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Samathan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/samathan/32/1058905_2.png) [@Samathan](https://forums.unrealengine.com/u/Samathan)\
**Post date:** [August 16, 2021, 3:27am UTC](https://forums.unrealengine.com/t/is-there-a-way-to-make-getallactorsofclass-return-an-array-of-a-class-instead-of-aactors/487445/3 "2021-08-16T03:27:51Z")

</div>

that worked perfectly, thanks!
