How to GetActorLocation() in c++ for "Target" object

Hi Everyone! This is likely a stupid question but I’m having a hard time with it. I am trying to convert some things I mocked up in blueprint to C++ and I’m stuck.

Basically I have a TankSentry enemy class that is derived from my player class. ATankSentry->ATank->APawn->…
I find this class in blueprint with GetAllActorsOfClass and pass it into a for each loop. I pass each element to my SeekAndSetNearestEnemy function from the for loop. This is all good and fine, issue is next.

201126-question2.jpg

This is where I am stuck, I do not know how to GetActorLocation of the TankSentry in c++, seems like I’m missing something simple yet but googling hasn’t helped narrow down the issue. Reason I need this is because I want to do a LineTrace from the player actors location with the enemy tanks location at each iteration of the for-loop.

This is what my code looks like:

void ATank::SeekAndSetNearestEnemy(ATankSentry* EnemyTank)
{
	auto enemy = EnemyTank;
	auto playerLocation = GetActorLocation();
	auto enemyLocation = enemy->GetActorLocation(); <<< THIS ERRORS OUT
        //Ray trace and so forth...
  
}

If EnemyTank is NULL or a nullptr, it will cause an error.

I am not 100% sure why you are using “auto”. I suggest using the actual type, since you already know what it’ll be.

// Make sure EnemyTank has a valid reference.
if(EnemyTank)
{
    FVector PlayerLocation = GetActorLocation( );
    FVector EnemyLocation = EnemyTank->GetActorLocation( );
    // Ray trace...
}

I figured it out, had some problems because of inheritance issues I think. I was trying to access the child class of ATank, ATankSentry within the ATank implementation. I changed it around so that the base class was passed in and then cast from within the function to the child class. To add clarity to my question, here is the solution:

void ATank::SeekAndSetNearestEnemy_new(ATank* EnemyTank)
{
	/* Ensure that a tracked vehicle exists.*/
	if (!EnemyTank) { return; }
	/* Ensure that the tracked vehicle is a TankSentry.*/
	ATankSentry* enemy = Cast<ATankSentry>(EnemyTank);
	if (!enemy) { return; }

	/*Get the player location and the location of the enemy tank.*/
	FVector playerLocation = GetActorLocation();
	FVector enemyLocation = enemy->GetActorLocation();
	
	/* Initialize the hit data that will be returned from the linetrace.*/
	FHitResult HitData(ForceInit);

	/* Line trace to see what we hit. */
	if (Trace(GetWorld(), this, playerLocation, enemyLocation, HitData, false) && HitData.GetActor())
	{
		/* Ensure that the actor hit is a Tank Sentry.*/
		if (enemy == Cast<ATankSentry>(HitData.GetActor())) {
			UE_LOG(LogTemp, Warning, TEXT("Found Enemy: %s"), *enemy->GetName())
			/* Get the distance between player and enemy.*/
			float distanceFromEnemy = HitData.Distance;
			/* Check to see if enemy is the closest target.*/
			if (distanceFromEnemy < ClosestTargetDistance)
			{
				/* If the enemy is the closest, set it as the target.*/
				ClosestTargetDistance = distanceFromEnemy;
				NearestTarget = enemy;
			}
		}
	}
}