Add A Custom Console Command via a Cheat Manager in C++

Let's add a simple console command that is customized to our specifications.

Unreal Engine 5 editor PIE Quinn standing in the gym with the GetPlayerName console command at the bottom and the output of the character name in the top left.
Quinn in the gym with a custom console command

Game Engine

Unreal Engine 5.5.3

IDE

Rider 2024.3.5

Project Name

MyProject

OS

macOS Sequoia 15.3

A CheatManager can be very helpful tool when debugging. A CheatManager can be added a to a PlayerController allowing us to add custom console commands or utilize pre-existing functions to help with common debugging interactions.

First, create a new CheatManager class that inherits from UCheatManager. Add the Exec function specifier to the UFUNCTION macro to create a custom command that we can use in the PIE or debug builds. For testing, I created a very simple GetPlayerName function to print the the third person template character's name.

MyCheatManager.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/CheatManager.h"
#include "MyCheatManager.generated.h"

UCLASS()
class MYPROJECT_API UMyCheatManager : public UCheatManager
{
	GENERATED_BODY()

	UFUNCTION(Exec)
	void GetPlayerName();
};

MyCheatManager.cpp

#include "MyCheatManager.h"
#include "MyProject/TP_ThirdPerson/TP_ThirdPersonCharacter.h"

void UMyCheatManager::GetPlayerName()
{

	AActor* MyActor = GetWorld()->GetFirstPlayerController()->GetPawn();
	ATP_ThirdPersonCharacter* MyCharacter = Cast<ATP_ThirdPersonCharacter>(MyActor);

	if (MyCharacter != nullptr)
	{
		FString PlayerName = MyCharacter->GetName();
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, *FString::Printf(TEXT("Character Name: %s"), *PlayerName));
		}
	}
}

Next, the new MyCheatManager class needs to be added to a PlayerController. I created a very rudimentary controller and the CheatClass variable to UMyCheatManager::StaticClass();.

MyPlayerController.h

#pragma once

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


UCLASS()
class MYPROJECT_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()

public:
	AMyPlayerController();
};

MyPlayerController.cpp

#include "MyPlayerController.h"
#include "MyProject/CheatManager/MyCheatManager.h"

AMyPlayerController::AMyPlayerController()
{
	CheatClass = UMyCheatManager::StaticClass();
}

The base UE third person template doesn't use a custom player controller so we need add the new MyPlayerController to the game mode. Inside TP_ThirdPersonGameMode set PlayerControllerClass to AMyPlayerController::StaticClass();.

TP_ThirdPersonGameMode.cpp

#include "TP_ThirdPersonGameMode.h"
#include "UObject/ConstructorHelpers.h"
#include "MyProject/PlayerController/MyPlayerController.h"

ATP_ThirdPersonGameMode::ATP_ThirdPersonGameMode()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/TP_ThirdPerson/Blueprints/BP_ThirdPersonCharacter"));
	if (PlayerPawnBPClass.Class != NULL)
	{
		DefaultPawnClass = PlayerPawnBPClass.Class;
	}

	PlayerControllerClass = AMyPlayerController::StaticClass();
}

After recompiling the project we can now use the GetPlayerName console command while playing the game. While playing in the editor, press the tilde ~ key to bring up the command input field and then input the desired command.

Unreal Engine 5 editor PIE Quinn standing in the gym with the GetPlayerName console command at the bottom and the output of the character name in the top left.
Using the custom console command `GetPlayerName`

I'm looking forward to using a CheatManager more in the future for development. I hope this helped.

Comments (0)

Add a Comment

Sign in to comment or like.

Delete Comment

-