Store an actor as a variable after Iterating through Scene Actors

I’m trying to find a certain actor, then save a reference to that actor so he can be referenced at any time. I found the following function for iterating through actors, but when I try and save ActorItr as an AActor variable, I get a conversion error: No conversion from TActorIterator to Actor exists.

       for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr )
    	{
    		//myActor = ActorItr;  (myActor is an AActor*) 
    	}

Makes sense, but, consistent with what I’ve learned about Unreal so far, I can’t find any further documentation on the subject. Is nobody else having this problem? It seems like a pretty standard function, to save an actor for later use. We do it in Unity all the time with GameObjects, without any problems.

In fact, a quick search for “How to find a game object in Unity” returns exactly what I need, breaking down the functionality and even showing how to assign that object to a variable: Unity - Scripting API: GameObject.Find

Wow, that’s handy!

Unreal on the other hand…: Google

Are people supposed to be born with this knowledge or does Unreal plan on doing any actual documentation in the future? Ranting aside, if anyone has an answer for this, I (and surely other developers considering using Unreal) would appreciate it!

Hi,

Try using :

for (TActorIterator ActorItr(GetWorld()); ActorItr; ++ActorItr ) { myActor = *ActorItr; }

You need to dereference the iterator to get the actual pointer, similar to iterators in c++
http://www.cplusplus.com/reference/iterator/

regards,

While the C++ API isn’t that well documented Unreal users do have access to the source. In this case you could’ve done a search for TActorIterator in the engine source and found around 100 cases where it was being used, e.g. looking at UCheatManager::DestroyPawns() would’ve made it perfectly clear how to obtain a pointer to the actor from the iterator.

I hadn’t considered searching the source code for references. Thanks for pointing that out, that will be a huge help in the future.

This was exactly what I needed, and I can’t believe I overlooked that minor detail. C++ isn’t my strongest point, but I should have known better there. Thanks for the reply!