Getting a Vector from my Player

Hey! I am pretty new to C++ programming in Unreal and was wondering how I could get this to work. I am trying to get a Vector from the players camera. I did the following:



FVector AWeapon::GetFirePosition()
{
	AFPSCharacter* pl = OwnerPawn ? Cast<AFPSCharacter>(OwnerPawn) : NULL;
	FVector FPos = pl->FirstPersonCameraComponent->GetComponentLocation();
	return FPos;
}

Which is inspired by this:



FVector AShooterWeapon::GetCameraAim() const
{
	AShooterPlayerController* const PlayerController = Instigator ? Cast<AShooterPlayerController>(Instigator->Controller) : NULL;
	FVector FinalAim = FVector::ZeroVector;

	if (PlayerController)
	{
		FVector CamLoc;
		FRotator CamRot;
		PlayerController->GetPlayerViewPoint(CamLoc, CamRot);
		FinalAim = CamRot.Vector();
	}
	else if (Instigator)
	{
		FinalAim = Instigator->GetBaseAimRotation().Vector();		
	}

	return FinalAim;
}

Its mainly the “?” and “Instigator->Controller” that are putting me off. How can I cast it so that it also works with my Character class? Would really appreciated it if someone helped me with this! Thank you in advance.

“?” is the ternary operator.

it works like this:



(Evaluation) ? (Do this if Evaluation is true) : (Do this if evaluation is false)


As for “Instigator->Controller”, Instigator is a pointer to some actor class, and Controller is a member of the Instigator class.
The operator “->” is sort of analogous to the dot “.” operator in that you can access members of a pointer.

The code can be rewritten as follows:



AShooterPlayerController* const PlayerController
FinalAim = FVector::ZeroVector;

// Check that Instigator is not null
if(Instigator)
{
    // Cast the Instigator's Controller (Instigator->Controller) to AShooterPlayerController
    PlayerController = Cast<AShooterPlayerController>(Instigator->Controller);
    
    // Check that our cast player controller is not null
    if(PlayerController)
    {
		FVector CamLoc;
		FRotator CamRot;
		PlayerController->GetPlayerViewPoint(CamLoc, CamRot);
		FinalAim = CamRot.Vector();
    }
    else
    {
		FinalAim = Instigator->GetBaseAimRotation().Vector();
    }
	
	return FinalAim;
}


So, you can replace



AShooterPlayerController


with your PlayerController class.

Hey, thanks for the reply and the explanation (it was very useful). Does that mean I have to create a controller class? Because at the moment I don’t have one (as far as I know).

No, you don’t need to create one. The game framework uses “PlayerController” by default, so you can try that.

Alright, thanks man! I am no longer getting any errors. I cant really say if its working (I have not implemented the function properly yet), but it seems to be working. Just one question: I assume that “Instigator” is just a variable that is automatically assigned to the player who calls the function? Anyway, here is the code I ended up using:


FVector AWeapon::GetFirePosition()
{
	AController* PlayerController;
	FVector FinalAim = FVector::ZeroVector;
	if (Instigator)
	{
		// Cast the Instigator's Controller (Instigator->Controller) to AShooterPlayerController
		PlayerController = Cast<AController>(Instigator->Controller);

		// Check that our cast player controller is not null
		if (PlayerController)
		{
			FVector CamLoc;
			FRotator CamRot;
			PlayerController->GetPlayerViewPoint(CamLoc, CamRot);
			FinalAim = CamRot.Vector();
		}
		else
		{
			FinalAim = Instigator->GetBaseAimRotation().Vector();
		}
	}
	return FinalAim;
}

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/Spawning/index.html

Instigator is of type Pawn, and seems like it can be set when spawning an actor - so when you spawn your weapon, just make sure to pass the pawn from which you’re spawning it into the spawn function.
You can use the “this” keyword to get a reference to your pawn instance inside your pawn class.

Ahhhh! Alright thank you very much!