Create a Quick Console Command in UE5 using C++
Create a Console Command using FAutoConsoleCommandWithWorldAndArgs in C++

Game Engine
Unreal Engine 5.5.3
IDE
Rider 2024.3.5
Project Name
MyProject
OS
macOS Sequoia 15.3
In a previous CheatManager post I talked about adding the Exec
specifier to a UFUNCTION
macro to add a custom console command, but there are alternative methods. One quick alternative technique is to use the FAutoConsoleCommandWithWorldAndArgs
function.
There are a lot of examples throughout the codebase and as I searched through I landed on the AssetManager.cpp
file for examples. FAutoConsoleCommandWithWorldAndArgs
takes four parameters listed below, taken directly from the function's comments inside IConsoleManager.h
- Name — The name of this command (must not be nullptr)
- Help — Help text for this command
- Command — The user function to call when this command is executed
- Flags — Optional flags bitmask
Inside MyCheatManager.cpp I added a very simple command called TestCommand
to print out argument strings. I gave it the name MyCheatManager.TestCommand
, so when I'm inside the PIE I can use the tilde ~
key to bring up the command prompt and fire my new custom function.
MyCheatManager.cpp
...
static FAutoConsoleCommandWithWorldAndArgs TestCommand(
TEXT("MyCheatManager.TestCommand"),
TEXT("Test Command in MyCheatManager"),
FConsoleCommandWithWorldAndArgsDelegate::CreateStatic([](const TArray<FString>& Params, UWorld* World)
{
if (Params.Num() == 0)
{
UE_LOG(LogTemp, Log, TEXT("No params listed"));
}
for (const FString& Param : Params)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, *FString::Printf(TEXT("Param Name: %s"), *Param));
}
}),
ECVF_Cheat
);
After recompiling we can now use our new command in PIE or other debug builds.

I found this to be a very cool way to quickly create a custom console command.

Harrison McGuire