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:


void UGameplayStatics::GetAllActorsOfClass(UObject* WorldContextObject, TSubclassOf ActorClass, TArray& OutActors)

	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;

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:


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