How to access an actor in a scene

Hey I was wondering the right way to get access to a class I created and put out into the scene. I made a variable for my custom class I created and put that variable in my character class. Though every time I go to assign it in the editor by the way of defaults it wont work. it just says my variable is none.
Here is my code


 UPROPERTY(EditAnywhere, Category = "MyComp")
AMyTestActor* myActorToSendTo;

Well, you only created an object pointer to AMyTestActor.

As far as I know, you can’t just pick an actor form your level in the DefaultProperties.

You have two options, instead of placing the actor in the scene, just spawn it using Blueprint or C++


myActorToSendTo = GetWorld()->SpawnActor<AMyTestActor>();

OR

Use ActorIterator to find a reference in the world:


for (TActorIterator<AMyTestActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
         myActorToSendTo = *ActorItr;
         break;
}

this code only returns the first reference that is found in the world and breaks out of the loop.

Alternatively, you could use the according Blueprint function to find a reference. It’s called GetAllACtorsOfClass.

Does TActorIterator go through and check every actor in the scene? If so what is the performance of that?

Well, as far as I know, Unreal keeps an actor map stored internally. So iterating over all actors of a type should be as fast as iterating over any other list/map. This being said, it still is iterating and shouldn’t be done every frame. But you won’t run into any problems if you only do it once to find a specific actor.