How to get a reference and store it

Hi,
I am trying to create a pawn and then get the reference to it using a TActorIterator.
The question is how get the reference from the TActorIterator object and store it in a pointer.

Here is the code
.h

// Called when the game starts or when spawned
    virtual void BeginPlay() override;
//object holding the reference to the player pawn
	ACameraBase* ControlledCamera;

.cpp

void ATC_PlayerController::BeginPlay()
{
	Super::BeginPlay();

	FVector Location = FVector::ZeroVector;
	FRotator Rotation = FRotator::ZeroRotator;

	FActorSpawnParameters Parameters;
	Parameters.Name =  "Camera";
	Parameters.Owner = this;

	this->GetWorld()->SpawnActor(ACameraBase::StaticClass(), &Location, &Rotation, Parameters);

	for (TActorIterator<ACameraBase> ActorItr(GetWorld()); ActorItr; ++ActorItr)
	{
		if (ActorItr->GetOwner() == this)
		{
			// here is where I should get the refrence from ActorItr and set it to ControlledCamera
		}
	}
}

if someone has a better way of doing this, please share it

Hi,

you get the pointer to the instance from UWorld::SpawnActor as a result.
There is no need to iterate through all actors.

ControlledCamera = Cast<ACameraBase> ( this->GetWorld()->SpawnActor(ACameraBase::StaticClass(), &Location, &Rotation, Parameters) )

Hope this helps :slight_smile: