Console commands in Unreal Engine are a practical way to expose debugging tools, testing hooks, and runtime controls without building UI. They are widely used during development to tweak gameplay, inspect state, and automate workflows.
What are console commands
A console command is a text instruction executed through the in game console. Unreal provides a system to register custom commands that map to C++ functions or variables.
They are typically used for:
- Debugging gameplay systems
- Changing variables at runtime
- Triggering test scenarios
- Profiling and diagnostics
Core systems involved
Console commands are handled through the IConsoleManager interface. You can register commands or variables that the engine exposes to the console.
There are two main approaches:
- Registering commands that execute functions
- Registering console variables that store and modify values
Registering a simple command
The most direct way is using FAutoConsoleCommand.
Example: respawning the player character
static FAutoConsoleCommand RespawnPlayerCommand(
TEXT("game.respawn"),
TEXT("Respawns the player character at the default spawn point"),
FConsoleCommandDelegate::CreateStatic([]()
{
if (GEngine && GEngine->GetWorldContexts().Num() > 0)
{
UWorld* World = GEngine->GetWorldContexts()[0].World();
if (!World) return;
APlayerController* PC = World->GetFirstPlayerController();
if (!PC) return;
APawn* Pawn = PC->GetPawn();
if (Pawn)
{
Pawn->Destroy();
}
World->GetAuthGameMode()->RestartPlayer(PC);
}
}));This registers a command called game.respawn that can be executed from the console.
Commands with parameters
You can define commands that accept arguments using FConsoleCommandWithArgsDelegate.
Example: setting player health manually
static FAutoConsoleCommand SetHealthCommand(
TEXT("game.set_health"),
TEXT("Sets player health. Usage: game.set_health 100"),
FConsoleCommandWithArgsDelegate::CreateStatic([](const TArray& Args)
{
if (Args.Num() == 0) return;
float NewHealth = FCString::Atof(*Args[0]);
if (GEngine && GEngine->GetWorldContexts().Num() > 0)
{
UWorld* World = GEngine->GetWorldContexts()[0].World();
if (!World) return;
APlayerController* PC = World->GetFirstPlayerController();
if (!PC) return;
ACharacter* Character = Cast(PC->GetPawn());
if (!Character) return;
UHealthComponent* HealthComp = Character->FindComponentByClass();
if (HealthComp)
{
HealthComp->SetHealth(NewHealth);
}
}
}));
Now you can run:
game.set_health 150Registering console variables
If you need persistent values that can be changed at runtime, use console variables.
Example: controlling enemy spawn rate
static TAutoConsoleVariable CVarEnemySpawnRate(
TEXT("game.enemy_spawn_rate"),
1.0f,
TEXT("Multiplier for enemy spawn rate"),
ECVF_Default
);You can read this value anywhere:
float SpawnRate = CVarEnemySpawnRate.GetValueOnGameThread();And change it in the console:
game.enemy_spawn_rate 2.0Binding commands to class methods
Instead of static lambdas, you can bind commands to object methods using CreateUObject.
Example inside a subsystem:
FAutoConsoleCommand DebugSpawnEnemyCommand;
void UEnemyDebugSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
DebugSpawnEnemyCommand = FAutoConsoleCommand(
TEXT("ai.spawn_enemy"),
TEXT("Spawns a test enemy at the player location"),
FConsoleCommandDelegate::CreateUObject(this, &UEnemyDebugSubsystem::SpawnEnemy)
);
}
void UEnemyDebugSubsystem::SpawnEnemy()
{
UWorld* World = GetWorld();
if (!World) return;
APlayerController* PC = World->GetFirstPlayerController();
if (!PC) return;
FVector Location = PC->GetPawn()->GetActorLocation();
World->SpawnActor(EnemyClass, Location, FRotator::ZeroRotator);
}This keeps the logic inside a proper system instead of global static code.
Execution context considerations
Console commands can run in different contexts:
- Editor
- PIE
- Standalone game
Always validate pointers like World, GameMode, or PlayerController to avoid crashes.
Best practices
- Use clear naming with prefixes like game, ai, debug
- Avoid heavy logic inside the command itself, delegate to systems
- Validate all inputs
- Avoid accessing world through unsafe assumptions
- Prefer subsystems or managers instead of static globals for complex logic
When to use console commands
- Testing gameplay systems quickly
- Debugging state without UI
- Exposing developer tools
- Tuning values during play sessions
Summary
Console commands are a low overhead and powerful way to interact with your game at runtime. They are especially useful for debugging, testing, and rapid iteration. With proper structure and validation, they can become an essential part of your development workflow.