If you need to find an object in the game world from a static context, where you cannot obtain the UWorld via some other means,
then the Object Iterator is the way to get the proper context and access the entire living game world!
Object Iterator Can Search for AActors
Because AActor extends UObject, the Object Iterator can search for AActors!
But the AActor Iterator cannot search for instances of UObjects that do not extend AActor at some point.
So the Object Iterator can do a search for all UStaticMeshComponents, as well as all ACharacters!
1
TObjectIterator<UStaticMeshComponent> Itr;
Copied!
1
TObjectIterator<ACharacter> Itr;
Copied!
Specifying Classes & Subclasses To Search For
Perhaps the most powerful feature of the Actor and Object Iterators is the ability to limit the scope of the search to a chosen base class and its subclasses!
This makes the iterator run faster and helps you gather only the data you really want from the game world!
ObjectIterator can and will return editor-instance / default object objects that simply should not be edited at runtime!
To filter out objects that you should not be editing at runtime, you can do a world check with an object that you know is part of the correct world (not the editor world)!
1
UWorld* YourGameWorld =//set this somehow, from another UObject or pass it in as parameter
2
​
3
for(TObjectIterator<UYourObject> Itr; Itr;++Itr)
4
{
5
//World Check
6
if(Itr->GetWorld()!= YourGameWorld)
7
{
8
continue;
9
}
10
//now do stuff
11
}
Copied!
In-Engine Example ~ Get All Widgets Of Class
The above is the code structure that I used for my Get All Widgets of Class node, pull request that Epic accepted that is now live in 4.7 !