How do I get a reference to an actor that's already in the scene?

I’m working on a tower defence kind of game. And I want enemies to move along a spline. So if I place an enemy and a spline into the scene, I can make that enemy move along that spline. Problem is that I have to select in the details panel of that enemy instance which spline to move along. Since there is going to be waves of enemies I need to spawn them during runtime. And if I spawn them during runtime I can’t select which spline to move along. Is there a way to set a default value for spline reference, or setting spline value in the details panel to a spline that is already in the scene during runtime.

You can use FindObject

FindObject<AMyActor>(GetWorld()->GetCurrentLevel(), TEXT("ActorName"));

Where ActorName is the name with the ID. You can get that by hovering over the actor in the world outliner.

There’s a third argument for whether or not to find exact class but I’m pretty sure it has a default argument so not required.

That worked thank you! For future reference I’m writing how did I use that function.

I have 2 classes EnemyMinion and Path. There are also blueprints derived from these classes. EnemyMinion is character blueprint and Path is actor blueprint. I attached a SplineComponent to Path.
Now in EnemyMinion.h I added

UPROPERTY(BlueprintReadOnly)
USplineComponent* SplineComponent;
UPROPERTY(VisibleAnywhere)
APath* SplinePath;

And in EnemyMinion.cpp

//This line gets the spline actor
SplinePath = FindObject<APath>(GetWorld()->GetCurrentLevel(), TEXT("BP_Path_2"));

//This one gets the SplineComponent attached to that actor
	if (SplinePath != nullptr)
		SplineComponent = Cast<USplineComponent>(SplinePath->GetComponentByClass(USplineComponent::StaticClass()));

And finally in EnemyMinion blueprint I called SplineComponent variable and attached it to the node responsible for moving the enemy along the spline.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.