The Unreal Engine allows you to use console commands during gameplay to change the behavior of the game during the gameplay.
You can execute console commands at any time during gameplay.
This is useful for debugging purposes.
Several useful console commands have already defined by default, and you can define your own console commands.
This article describes how to define console commands using CheatManager.
What are console commands?
Console commands are commands that can be executed on the level editor viewport.
You can display the console by pressing the key ““ on the level editor viewport.
However, “` cannot be entered from some keyboard layouts.
You need to add the appropriate keys from [Project Settings] > [Input] > [Console] > [Console Keys].
Several console commands are provided by default.
For example, the Stat command is a console command that can display CPU and GPU execution times for each process.
This is a very useful console command when you look for the processes that are performance bottlenecks in your game.
You may want to enable this function to evaluate performance and display only when you need.
The console command, which can be enabled/disabled at any time during gameplay, matches this purpose.
Define Console Commands using CheatManager
To define a console command, you need to add exec
to the argument of the UFUNCTION
macro.
UFUNCTION(exec)
void SAMPLE_Command();
However, you can add an argument exec
to the UFUNCTION
macro only to following classes.
- Pawn
- PlayerController
- CheatManager
- GameMode
- PlayerInput
- HUD
We will define console commands in CheatManager.
CheatManager is a class with various debugging features, which automatically disabled when you packages the game with the release configuration.
For this reason, CheatManager is the suitable target for implementing console commands for debugging purposes.
To learn more about CheatManager, please refer to the official documentation.
Example
Let’s create our own class inherited from CheatManager and define a console command to it.
This time, we will define a console command SAMPLE_OutputLog
that receives a string as a command argument and outputs the received string to a log.
1. Define a Console Command
CheatManager is defined as UCheatManager
class in GameFramework/CheatManager.h
.
Define UOutputLogConsoleCommandManager
class derived from UCheatManager
class.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/CheatManager.h"
#include "OutputLogConsoleCommand.generated.h"
UCLASS()
class REFERENCEPROEJCT_API UOutputLogConsoleCommandManager : public UCheatManager
{
GENERATED_BODY()
public:
// Define a console command "SAMPLE_OutputLog".
UFUNCTION(exec)
void SAMPLE_OutputLog(FString Message);
};
You can define a console command by adding the macro UFUNCTION(exec)
to the member function SAMPLE_OutputLog
defined in UOutputLogConsoleCommandManager
.
Note that the name of the member function becomes the console command name.
The SAMPLE_OutputLog
console command accepts a string as a command argument, so the argument Message
with type FString
is added to the member function SAMPLE_OutputLog
.
The argument of the member function is the command argument of the console command.
Next, we will implement the member function SAMPLE_OutputLog
.
The UE_LOG
macro is used to output the string received as the command argument to the log.
#include "OutputLogConsoleCommand.h"
void UOutputLogConsoleCommandManager::SAMPLE_OutputLog(FString Message)
{
UE_LOG(LogTemp, Log, TEXT("Message: %s"), *Message);
}
2. Enable CheatManager
The UOutputLogConsoleCommandManager
class must be enabled before you can use it.
To enable UOutputLogConsoleCommandManager
class, you must create your own PlayerController and specify UOutputLogConsoleCommandManager
class in the member variable CheatClass
.
To implement this in Bluepirnt, inherit PlayerController and set OutputLogConsoleCommandManager
to the Cheat Class in the Details tab.
To implement in C++, add UOutputLogConsoleCommandManager::StaticClass()
to the member variable CheatClass
in the constructor of the class derived from PlayerController.
Below code shows the implementation of the AOutputLogConsoleCommandPlayerController
class derived from PlayerController.
AOutputLogConsoleCommandPlayerController::AOutputLogConsoleCommandPlayerController(const FObjectInitializer& ObjectInitializer)
{
CheatClass = UOutputLogConsoleCommandManager::StaticClass();
}
3. Create GameMode
To use the created PlayerController during gameplay, create a new GameMode (in this case BP_OutputLogConsoleCommandGameMode
) in Blueprint and set the PlayerController to OutputLogConsoleCommandPlayerController
.
Set the newly created GameMode to the level.
4. Check
Display the console while gameplay and type SAMPLE_OutputLog Hoge
.
Press Enter to execute the console command, and you will see LogTemp: Message: Hoge
in the [Output Log].