Unreal Actor Iterators - How to use them, when and when maybe not to use them - UE C++ Tutorial

preview_player
Показать описание
Actor Iterators allow you to iterate over the actors in a world for a given class.
This is extremely convenient when coupling them with console commands for debugging purposes.
While they are pretty optimized, I dig into the code behind them in the public engine source code.
It appears to me that there is quite a bit of overhead, just from reading the code, of using an actor iterator.
So I'd suggest you use them mostly for debugging and testing purposes, not gameplay code.
If you need something similar for gameplay code, one approach is to have actors register with a gamestate component on begin play.
And have them unregister on end play.
This will allow you to keep a global array of actors of a given class, without needing to use reflection.
Nevertheless, Actor Iterators are extremely convenient, and are worth adding to your tool belt.

0:00 Setting up a console command to test Actor Iterators ( TActorIterator )
0:09 Defining the actor iterator boilerplate
0:46 Testing an actor iterator
1:22 Where to use an actor iterator
1:30 Exploring the performance cost of using actor iteators
2:18 -- GetObjectsOfClass explored
2:53 -- ForEachObjectOfClass explored
3:09 -- RecursivelyPopulateDerivedClasses explored
4:05 -- ForEachObjetOfClasses_Implementation explored
4:52 My personal thoughts on performance - only use them for debug code
5:18 An easy workaround using gamestate, to achieve something similar to actor iterators in gameplay code
5:57 Something to investigate if needed
6:08 I primarily use Actor Iterators for debug code, not game code

I did a local test changing the TMap code slightly. In my local test I added a unique object for the value, distinct from the unique object for the key. Both objects were protected from the garbage collector. These were the results:
```
Log LogTemp [GCTest_EvaluateObjects]: Object appears valid [0x56bdef24290][0x56bdef25940] in SAFE MAP
Log LogTemp [GCTest_EvaluateObjects]: the KEY weak pointer has address: [0x56bdef24290]
Log LogTemp [GCTest_EvaluateObjects]: the VALUE weak pointer has address: [0x56bdef25940]
Log LogTemp [GCTest_EvaluateObjects]: Object appears valid [0x56bdef20c00][0x56bdef23ff0] in UNSAFE MAP
Log LogTemp [GCTest_EvaluateObjects]: the KEY weak pointer has address: [(nil)]
Log LogTemp [GCTest_EvaluateObjects]: the VALUE weak pointer has address: [(nil)]
```
Рекомендации по теме
Комментарии
Автор

I've never dug into it too closely. Does it actually iterate over every actor in the world? Or does the engine maintain a subset list on a per class basis? I always assumed it was a 'bad perf iterates every actor' but it almost looks like it might be managing a list of every class?

namrog
welcome to shbcf.ru