How to use APlayerController::GetAudioListenerPosition

I’m new to C++ and try to learn, but one thing I want to do is to use “GetAudioListenerPosition” but I can’t get it to work.
Can someone tel me what the code is and tell me what codes goes in the .h file and what codes goes in the .cpp file and wat includes I need?

I can get this in Blueprint, but I want to get it in C++

Why can’t you look inside the PlayerController class and see the code for yourself?
If you’re taken aback by out parameters (the ones marked with &), it’s not hard.
Just declare three empty vectors:

FVector OutLocation;
FVector OutFrontDir;
FVector OutRightDir;

and call the function of the player controller with them:

GetAudioListenerPosition(OutLocation, OutFrontDir, OutRightDir);

Now your vectors have values.

P.S. You need to #include "GameFramework/PlayerController.h" to use its functions.

1 Like

When you say PlayerController, do you mean the Character.cpp, Character.h or is it in a different code?

I mean PlayerController.h and .cpp

I get 10 errors and 4 warrnings, can you help me with that?

Severity Code Description Project File Line Suppression State
Warning C4264 ‘void APlayerController::GetAudioListenerPosition(FVector &,FVector &,FVector &) const’: no override available for virtual member function from base ‘APlayerController’; function is hidden TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 24
Error C4596 ‘GetAudioListenerPosition’: illegal qualified name in member declaration TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 23
Error C2061 syntax error: identifier ‘OutLocation’ TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 23
Warning C4263 ‘void AMyPlayerController::GetAudioListenerPosition(void)’: member function does not override any base class virtual member function TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 23
Error C2065 ‘OutLocation’: undeclared identifier TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp 7
Error C2065 ‘OutFrontDir’: undeclared identifier TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp 7
Error C2065 ‘OutRightDir’: undeclared identifier TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp 7
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp 7
Error C2440 ‘initializing’: cannot convert from ‘initializer list’ to ‘int’ TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp 7
Error C4596 ‘GetAudioListenerPosition’: illegal qualified name in member declaration TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 23
Error C2061 syntax error: identifier ‘OutLocation’ TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 23
Warning C4263 ‘void AMyPlayerController::GetAudioListenerPosition(void)’: member function does not override any base class virtual member function TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 23
Warning C4264 ‘void APlayerController::GetAudioListenerPosition(FVector &,FVector &,FVector &) const’: no override available for virtual member function from base ‘APlayerController’; function is hidden TestNewAudio02 E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h 24
Error MSB3073 The command D:\Epic Games\Epic Games\UE\UE_5.0\UE_5.0\Engine\Build\BatchFiles\Build.bat TestNewAudio02Editor Win64 Development -Project=E:\UE\5S\TestNewAudio02\TestNewAudio02.uproject -WaitMutex -FromMsBuild exited with code 6. TestNewAudio02 D:\VS2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.MakeFile.Targets 45
1 Like

here are my codes:
.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "Runtime/Engine/Classes/AI/Navigation/NavigationTypes.h"
#include "MyPlayerController.generated.h"


/**
 * 
 */
UCLASS()
class TESTNEWAUDIO02_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()
	


	void AMyPlayerController::GetAudioListenerPosition(OutLocation, OutFrontDir, OutRightDir) const override;
};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPlayerController.h"
#include "GameFramework/PlayerController.h"

void AMyPlayerController::GetAudioListenerPosition(OutLocation, OutFrontDir, OutRightDir)
{
	FVector OutLocation;
	FVector OutFrontDir;
	FVector OutRightDir;
}

and 13 errors:

Severity	Code	Description	Project	File	Line	Suppression State
Error	C4596	'GetAudioListenerPosition': illegal qualified name in member declaration	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Error	C2061	syntax error: identifier 'OutLocation'	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Error	C3668	'AMyPlayerController::GetAudioListenerPosition': method with override specifier 'override' did not override any base class methods	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Warning	C4263	'void AMyPlayerController::GetAudioListenerPosition(void) const': member function does not override any base class virtual member function	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Warning	C4264	'void APlayerController::GetAudioListenerPosition(FVector &,FVector &,FVector &) const': no override available for virtual member function from base 'APlayerController'; function is hidden	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	22	
Error	C2065	'OutLocation': undeclared identifier	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp	7	
Error	C2065	'OutFrontDir': undeclared identifier	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp	7	
Error	C2065	'OutRightDir': undeclared identifier	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp	7	
Error	C2761	'void AMyPlayerController::GetAudioListenerPosition(void) const': redeclaration of member is not allowed	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp	7	
Error	C2143	syntax error: missing ';' before '{'	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp	8	
Error	C2447	'{': missing function header (old-style formal list?)	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.cpp	8	
Error	C4596	'GetAudioListenerPosition': illegal qualified name in member declaration	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Error	C2061	syntax error: identifier 'OutLocation'	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Error	C3668	'AMyPlayerController::GetAudioListenerPosition': method with override specifier 'override' did not override any base class methods	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Warning	C4263	'void AMyPlayerController::GetAudioListenerPosition(void) const': member function does not override any base class virtual member function	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	21	
Warning	C4264	'void APlayerController::GetAudioListenerPosition(FVector &,FVector &,FVector &) const': no override available for virtual member function from base 'APlayerController'; function is hidden	TestNewAudio02	E:\UE\5S\TestNewAudio02\Source\TestNewAudio02\MyPlayerController.h	22	
Error	MSB3073	The command ""D:\Epic Games\Epic Games\UE\UE_5.0\UE_5.0\Engine\Build\BatchFiles\Build.bat" TestNewAudio02Editor Win64 Development -Project="E:\UE\5S\TestNewAudio02\TestNewAudio02.uproject" -WaitMutex -FromMsBuild" exited with code 6.	TestNewAudio02	D:\VS2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.MakeFile.Targets	45	

What you send me looks like it comes from the documentation, But I don’t know how to add thoes code to my codes, and I have no idea how to code it

  1. GetAudioListenerPosition already exists in the player controller class, you must not create your own;
  2. In MyPlayerController.h, declare vectors::
FVector OutLocation;
FVector OutFrontDir;
FVector OutRightDir;
  1. In MyPlayerController.cpp, now you can call it in any function, maybe on BeginPlay() or whatever:
    GetAudioListenerPosition(OutLocation, OutFrontDir, OutRightDir);
    and your three vectors will be updated.

Where do I find the GetAudioListenerPosition in this code?

.h

// Copyright Epic Games, Inc. All Rights Reserved.

#include "NewTestCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"

//////////////////////////////////////////////////////////////////////////
// ANewTestCharacter

ANewTestCharacter::ANewTestCharacter()
{
	// Set size for collision capsule
	GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);

	// set our turn rate for input
	TurnRateGamepad = 50.f;

	// Don't rotate when the controller rotates. Let that just affect the camera.
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	// Configure character movement
	GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...	
	GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate

	// Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint
	// instead of recompiling to adjust them
	GetCharacterMovement()->JumpZVelocity = 700.f;
	GetCharacterMovement()->AirControl = 0.35f;
	GetCharacterMovement()->MaxWalkSpeed = 500.f;
	GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
	GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;

	// Create a camera boom (pulls in towards the player if there is a collision)
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(RootComponent);
	CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character	
	CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller

	// Create a follow camera
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
	FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm

	// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character) 
	// are set in the derived blueprint asset named ThirdPersonCharacter (to avoid direct content references in C++)
}

//////////////////////////////////////////////////////////////////////////
// Input

void ANewTestCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
	// Set up gameplay key bindings
	check(PlayerInputComponent);
	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

	PlayerInputComponent->BindAxis("Move Forward / Backward", this, &ANewTestCharacter::MoveForward);
	PlayerInputComponent->BindAxis("Move Right / Left", this, &ANewTestCharacter::MoveRight);

	// We have 2 versions of the rotation bindings to handle different kinds of devices differently
	// "turn" handles devices that provide an absolute delta, such as a mouse.
	// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
	PlayerInputComponent->BindAxis("Turn Right / Left Mouse", this, &APawn::AddControllerYawInput);
	PlayerInputComponent->BindAxis("Turn Right / Left Gamepad", this, &ANewTestCharacter::TurnAtRate);
	PlayerInputComponent->BindAxis("Look Up / Down Mouse", this, &APawn::AddControllerPitchInput);
	PlayerInputComponent->BindAxis("Look Up / Down Gamepad", this, &ANewTestCharacter::LookUpAtRate);

	// handle touch devices
	PlayerInputComponent->BindTouch(IE_Pressed, this, &ANewTestCharacter::TouchStarted);
	PlayerInputComponent->BindTouch(IE_Released, this, &ANewTestCharacter::TouchStopped);
}

void ANewTestCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
{
	Jump();
}

void ANewTestCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
{
	StopJumping();
}

void ANewTestCharacter::TurnAtRate(float Rate)
{
	// calculate delta for this frame from the rate information
	AddControllerYawInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
}

void ANewTestCharacter::LookUpAtRate(float Rate)
{
	// calculate delta for this frame from the rate information
	AddControllerPitchInput(Rate * TurnRateGamepad * GetWorld()->GetDeltaSeconds());
}

void ANewTestCharacter::MoveForward(float Value)
{
	if ((Controller != nullptr) && (Value != 0.0f))
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}

void ANewTestCharacter::MoveRight(float Value)
{
	if ( (Controller != nullptr) && (Value != 0.0f) )
	{
		// find out which way is right
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);
	
		// get right vector 
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		// add movement in that direction
		AddMovementInput(Direction, Value);
	}
}

.cpp

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "NewTestCharacter.generated.h"

UCLASS(config=Game)
class ANewTestCharacter : public ACharacter
{
	GENERATED_BODY()

	/** Camera boom positioning the camera behind the character */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent* CameraBoom;

	/** Follow camera */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent* FollowCamera;
public:
	ANewTestCharacter();

	/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Input)
	float TurnRateGamepad;

protected:

	/** Called for forwards/backward input */
	void MoveForward(float Value);

	/** Called for side to side input */
	void MoveRight(float Value);

	/** 
	 * Called via input to turn at a given rate. 
	 * @param Rate	This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
	 */
	void TurnAtRate(float Rate);

	/**
	 * Called via input to turn look up/down at a given rate. 
	 * @param Rate	This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
	 */
	void LookUpAtRate(float Rate);

	/** Handler for when a touch input begins. */
	void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);

	/** Handler for when a touch input stops. */
	void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);

protected:
	// APawn interface
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	// End of APawn interface

public:
	/** Returns CameraBoom subobject **/
	FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
	/** Returns FollowCamera subobject **/
	FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
};


Cast<APlayerController>(GetController())->...

You might need to #include "GameFramework/PlayerController.h" too.

Why are you doing this, anyway?

to update audio on the player model or capsule, if I don’t code this, den the player can move or rotate the camera into audio and out. something I don’t want.

I can’t find out how to code it.

If you’re not too familiar with C++, why not just do in it blueprints?
image