# How does TActorIterator work internally?

**URL:** <https://forums.unrealengine.com/t/how-does-tactoriterator-work-internally/2139688>\
**Category:** C++\
**Tags:** unreal-engine, CPP, question\
**Created:** [November 21, 2024, 3:10am UTC](https://forums.unrealengine.com/t/how-does-tactoriterator-work-internally/2139688 "2024-11-21T03:10:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan3z](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ivan3z](https://forums.unrealengine.com/u/Ivan3z)\
**Post date:** [November 21, 2024, 3:10am UTC](https://forums.unrealengine.com/t/how-does-tactoriterator-work-internally/2139688/1 "2024-11-21T03:10:34Z")

</div>

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.

```auto
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!!

---

<div class="post-metadata">

**Author:** ![MagForceSeven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/magforceseven/32/906563_2.png) [@MagForceSeven](https://forums.unrealengine.com/u/MagForceSeven)\
**Post date:** [November 21, 2024, 3:27pm UTC](https://forums.unrealengine.com/t/how-does-tactoriterator-work-internally/2139688/2 "2024-11-21T15:27:54Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![uahcna](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@uahcna](https://forums.unrealengine.com/u/uahcna)\
**Post date:** [November 22, 2024, 8:27am UTC](https://forums.unrealengine.com/t/how-does-tactoriterator-work-internally/2139688/3 "2024-11-22T08:27:48Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/6/0/b601f42a80bd9b4d9fe04f6af8584316e437997d.png)  
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.

---

<div class="post-metadata">

**Author:** ![Ivan3z](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ivan3z](https://forums.unrealengine.com/u/Ivan3z)\
**Post date:** [November 23, 2024, 8:00pm UTC](https://forums.unrealengine.com/t/how-does-tactoriterator-work-internally/2139688/4 "2024-11-23T20:00:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [December 23, 2024, 8:01pm UTC](https://forums.unrealengine.com/t/how-does-tactoriterator-work-internally/2139688/5 "2024-12-23T20:01:58Z")

</div>

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