How to use the Player Controller class in C++

Hey guys,

I’m trying to figure out how exactly to use the player controller class in C++. I’m reading Rama’s guide on implementing a line trace via code, but can’t seem to figure out how to manipulate the PC class.

In essence, I would like some pointers on how to use the UE4 player controller class. How to add to it/use it in a custom player character class/if it’s possible to make a custom player controller.

Depending upon what you’re doing, the CharacterMovementComponent has a lot of built in functionality that is helpful. Although, if you don’t need all of it’s functionality, it can be a total pain. When working with it, I spend more time fiddling with booleans than actually coding. But, if you need those built in features right out of the box, it’s great.

By default, it’s contained in any ACharacter class, and all you have to use is GetCharacterMovement() to retrieve a pointer.

Create a custom controller shouldn’t be too difficult either, just create a new class from the C++ wizard in the editor, derive from CharacterMovement, and bam. I don’t know exactly how you would override ACharacter’s already set MovementComponent, though, since it is private.

Player Controller and Character Movement are completely separate classes.
If you want to make a custom player controller, you need to derive from APlayerController.
APlayerController is an AActor, and part of the Game Framework.
UCharacterMovementComponent is an optional component of a Character/Pawn/Actor.

You don’t actually add your Player Controller (PC) to your Character. The Character exists on its own and the PC acts upon it. The Pawn/Character shouldn’t know or care if it’s being controlled by a APlayerController or an AAIController, or any customized subclass.

To create a custom Player Controller, derive from APlayerController and customize however you wish.

To load your custom PC, you need to modify your Game Mode. You can do this in the editor by using the drop-down selections within your game mode actor in the content browser, or you can customize it within the details panel of an individual level.

You can also preset custom defaults within your C++ game mode constructor.

e.g.

ACustomPlayerController.h

#include "Runtime/Engine/Classes/GameFramework/PlayerController.h"
    
#include "ACustomPlayerController.generated.h"

/**
*
*/
UCLASS(Blueprintable, BlueprintType)
class MYGAME_API ACustomPlayerController : public APlayerController
{
	GENERATED_BODY()

	//Constructor
	ACustomPlayerController();

        //... custom code ...

};

ACustomPlayerController.cpp

#include "MyGame.h"

#include "ACustomPlayerController.h"
    
//Constructor
ACustomPlayerController::ACustomPlayerController()
	:
	APlayerController()
{
    //... custom defaults ...
}

//... custom code ...

ACustomGameMode.h

#include "GameFramework/GameMode.h"

#include "ACustomPlayerController.h"

/**
*
*/
UCLASS(Blueprintable, BlueprintType)
class MYGAME_API ACustomGameMode : public AGameMode
{
	GENERATED_BODY()

	//Constructor
	ACustomGameMode();

        //... custom code ...

};

ACustomGameMode.cpp

#include "MyGame.h"

#include "ACustomGameMode.h"
   
//Constructor
ACustomGameMode::ACustomGameMode()
	:
	AGameMode()
{

    //tell your custom game mode to use your custom player controller
	PlayerControllerClass = ACustomPlayerController::StaticClass();

	//you can set whatever (if any) other default framework classes
    //you wish for this game mode as well
	DefaultPawnClass = ACustomPawn::StaticClass();
	GameStateClass = ACustomGameState::StaticClass();
	HUDClass = ACustomGameHUD::StaticClass();
	ReplaySpectatorPlayerControllerClass = ACustomReplaySpectatorPlayerController::StaticClass();
	SpectatorClass = ACustomSpectatorClass::StaticClass();

}

//... custom code ...
3 Likes

Thanks for the answers guys

Woah, I must have had a stroke or something, I don’t know why I thought the OP was talking about the MovementComponent. My bad.