How does TActorIterator work internally?

I want to know how TActorIterator works internally.
It’s a matter of efficiency.

I mean. Suppose I want to search for a specific type of actor.

for (TActorIterator<AActor> ItActor = TActorIterator<AActor>(World, ASomeActorType::StaticClass()); ItActor; ++ItActor)
{

}

I can imagine this can work two ways.

1-Searches for all actors in the level and returns those of the specified class.(more inefficient way).

2-The engine has already mapped the actors by class previously. In this case it don’t need to check all the actors in the level (more efficient way).

Well, if I know how TActorIterator works internally I can decide whether to use it directly or make my own actor map insted.

Does anyone know?

Thank you so much!!

It’s number 2, the engine already has them bucketed by type.

You can also do:
for (TActorIterator<ASomeActorType> ItActor = TActorIterator<ASomeActorType>(World); ItActor; ++ItActor)
or
for (ASomeActorType* Actor : TActorRange<ASomeActorType>(World))

I’m not 100% sure if it has buckets per-world or not. It may iterate all actors and skip those not in the right world, but that’s only a perf problem in the Editor so can usually be ignored. Of course it also matters when/how often you’re planning on doing this iteration and now big the collection might get but overall it’s fast enough to not worry about it in general.

TObjectIterator works the same way (pre-bucketed). You have to do your own world filtering though when that’s relevant (like iterating any actor component type).

1 Like


After reviewing the engine’s source code, it appears that if the template type is ‘AActor’, the first method will be used. Otherwise, the engine retrieves the object from the typed bucket. These buckets are not specific to each world, so the engine will iterate through all actors and skip those that are not in the correct world.

1 Like

Thank you very much for your answers @MagForceSeven and @uahcna

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.