I’m sorry if this is badly put. Here is my issue
I use GetAllActorsOfClass to get all actors in the scene of the ASomeActor class
It outputs to an array of AActor
I want it to output to an array of ASomeActor
I hope this makes sense. Any help would be greatly appreciated 
I don’t think there is a way to cast an entire array’s contents easily. You could try this method, though:
TArray<AActor*> ActorArray;
TArray<ASomeActor*> SomeActorArray;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASomeActor::StaticClass(), ActorArray);
for (AActor* UncastedActor : ActorArray) {
SomeActorArray.Add(Cast<ASomeActor>(UncastedActor));
}
// SomeActorArray is now an array of ASomeActor.
Beware, don’t run this every frame as it is somewhat expensive to run.
If you only need this functionality from C++ and you plan to do this for more than 1 actor class, you can create a templated version of GetAllActorsOfClass. The implementation below is similar to UGameplayStatics::GetAllActorsOfClass but omits the ActorClass==nullptr check since the compiler will make sure ActorClass is valid.
#include "EngineUtils.h"
template <typename ActorClass>
void TGetAllActorsOfClass(UWorld* World, TArray<ActorClass*>& OutActors)
{
OutActors.Reset();
if (World)
{
for (TActorIterator<ActorClass> It(World); It; ++It)
{
OutActors.Add(*It);
}
}
}
void Test(UWorld* World)
{
TArray<APawn*> MyPawns;
TGetAllActorsOfClass(World, MyPawns);
TArray<ACharacter*> MyCharacters;
TGetAllActorsOfClass(World, MyCharacters);
/*
Won't compile because UObject not a sub-class of AActor.
TArray<UObject*> MyObjects;
TGetAllActorsOfClass(World, MyObjects);
*/
}
Note this function cannot be used from Blueprints. If you need to create typed arrays accessible from blueprints, you’ll need to make a blueprint library function for each array type you want to get actors for.
2 Likes