C++ Find Specific Actor in Scene

Is there a way to find an actor in the scene, be it by type, name or state? There surely is, but it has thus far eluded me.

#Actor Iterator

Here’s my tutorial on your question!

Actor and Object Iterator!

Wiki Link
A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine ForumsObject%26_Actor_Iterators,_Optional_Class_Scope_For_Faster_Search

But what if I have 100500 actors in my scene. Do I have to search my actor from loop, checking every actor?

You can use naming conventions to make your search much faster. Basically… search for the first few letters of what you’re looking for and organize it that way. So basically you can set the iterator like this…

It’ll narrow the search quite abit.


Here is an example of how to use it.

		FString A = "hey";

		if (A.Contains(FString("he")))
			GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Yellow, FString::FString("FOUND IT!!!!"));

			GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Yellow, FString::FString("NOPE!!!!!"));

But it’s all the same loop. Loop is long way.

Hi ,

Unfortunately since the data structure being used to hold the AActors is a linked list, looping through with iterators is probably the only way.

EDIT: chalk the above to my inexperience with Unreal’s core code but you could probably try something like this

#include <NewActor.hpp> // your actor

void somefunc()
    for (TActorIterator<AActor*> ActorItr(GetWorld(), NewActor::GetClass()); ActorItr; ++ActorItr)
        // do stuff

TActorIterator constructor is able to take in a UClass type to get actors of (in this case NewActor::GetClass(), derived from UObjectBase). Specifying your class type would reduce your overall array length considerably, giving you a much smaller search space to loop through.

Hope this helps

Don’t use Contains() if you’re worried about speed, because it has to scan the ENTIRE string looking for the substring. If you take the trouble to form your search string so it’s an exact match for the target object, you can just compare it against each object name and it will early out on the first letter that doesn’t match.