Referencing Actor in world using c++

It depends, what actor do you need? If you want to filter all actors by the display name you can edit the code as follows:

TSubclassOf<AActor> ActorClass;
TArray<AActor*> OutActors;
if (ActorClass)
{
	for(TActorIterator<AActor> It((), ActorClass); It; ++It)
	{
		AActor* Actor = *It;
		if(Actor->GetDisplayName == "MyActor")
		{
			OutActors.Add(Actor);
		}
	}
}

GetActorOfClass() returns the first actor found of a specific class instead of all actors, so if you have 2 actors in the world, it will return only the first found. If you want to call directly these functions from C++ instead of using TActorIterator you can #include “Kismet/GameplayStatics.h” and then call directly them like this:

TSubclassOf<AActor> ActorClass;
TArray<AActor*> OutActors;
UGameplayStatics::GetAllActorsOfClass((), ActorClass, OutActors);
1 Like