What does Get all Actors of class do?

Specifically how does it come to the list of actors? Is there a registry and it just reads from it; or it computes out of all actors which are the ones you are looking for? Because if it’s the latter I’m better off making a GlobalVariableHolderActor which holds all actors in their arrays and updates on request. I’m making a lot of get all actors.

I cannot answer specifically, how that function is doing it. Yet, unless you are injecting a lot of new actors, into the game all the time, you are better off, getting the list of actors one time, that you are concerned about, rather than querying all the time. Because no matter how efficient, the Get All Actors of Class is, it cannot be faster, than just holding a simple array that you maintain, for those actors you care about. If the Actors are constantly coming and going from the game, then that has to be balanced with keeping your own array. If the actors are relatively static, make your own array, and get on with getting on.

.

AFAIK Get All Actors of class checks every actor in memory and returns them using the specified class as a filter. I may be wrong though. Still, you’re way better off caching a reference.

Why dont you check by yourself:

https://github.com/EpicGames/UnrealEngine/blob/dff3c48be101bb9f84633a733ef79c91c38d9542/Engine/Source/Runtime/Engine/Private/GameplayStatics.cpp#L584

void UGameplayStatics::GetAllActorsOfClass(UObject* WorldContextObject, TSubclassOf ActorClass, TArray& OutActors)
{
	OutActors.Empty();

	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);

	// We do nothing if not class provided, rather than giving ALL actors!
	if (ActorClass && World)
	{
		for(TActorIterator<AActor> It(World, ActorClass); It; ++It)
		{
			AActor* Actor = *It;
			if(!Actor->IsPendingKill())
			{
				OutActors.Add(Actor);
			}
		}
	}
}

It using actor iterator to iterate thru all actors in the world and add actors to array that macth the conditions, looking at iterator code:

https://github.com/EpicGames/UnrealEngine/blob/dff3c48be101bb9f84633a733ef79c91c38d9542/Engine/Source/Runtime/Engine/Public/EngineUtils.h#L226

It tracks all spawned actors and loop thru them, so indeed if you have big world lot of actors this might eat performance, best is to avoid use of it if it;s possible, when you spawn something keep refrence of that wou spawn and register in specific classes that need to know about existance of that actor

FYI - For future users, I thought it would be important to note that GetAllActorsOfClass DOES NOT iterate over all actors. It only iterates overe a hash of all actors of that class…

See http://www.casualdistractiongames.com/single-post/2016/09/15/Inside-UE-Source-FUObjectHashTables-the-magic-behind-GetAllActorsWith

1 Like