IConsoleManager

Using IConsoleManager to handle and add custom console commands and variables

What is the IConsoleManager?

Explain the difference between IConsoleManager and FConsoleManager (They both seem to do the same thing? Except that the one is a struct the other a class)

The IConsoleManager is a singelton struct that handles and manages console commands and variables, each console command & variable are of type IConsoleObject, the manager handles all of these objects and provides functions to manipulate, register & un-register them.

// Include for the Console Manager, present in the Core module.
#include "HAL/IConsoleManager.h"

While there are simple UFUNCTIONS with an exec specifier they do not provide as much of greater control compared to the manager, the manager can register commands with special characters like "c.Test" as well as provide additional information about the command itself, finally it can be executed with a variety of methods such as Lambda expressions, delegates or use unreals reflection system to execute UFUNCTIONS.

Implementing a custom console command

For this example we will use a class inherting from UCheatManager as its the most appropriate location to add console commands and general debugging cheats as well as easy being to instantiate as it only has to be added to the player controller.

NOTE: When adding a console command make sure they don't exist already!

Example in C++ | No Arguments

// Header
#pragma once

#include "CoreMinial.h"
#include "HAL/IConsoleManager.h"

/**
* CheatManager that contains our console commands
*/
UCLASS(Blueprintable)
class YOUR_API UConsoleCheats : public UCheatManager
{
     GENERATED_BODY()

protected:

     virtual void InitCheatManager() override;
}

We will be adding our console command as a global static variable of type FAutoConsoleCommands, this will automatically add our commands and ensure we don't register the same command twice again.

// CPP
#include "ConsoleCheats.h"

static FAutoConsoleCommand CustomCommand
(
    /*Name*/TEXT("c.CustomCommand"),
    /*Info*/TEXT("A custom command that prints hello world!"),
    /*Func*/FConsoleCommadDelegate::CreateLambda([] () { UE_LOG(LogTemp, Display, TEXT("Hello World!")) })
);

void UConsoleCheats::InitCheatManager()
{
   ReceiveInitCheatManager();
}

Adding all of your console commands into a single cpp file is probably a bad idea, try to encapsulate them and add them according to where they make sense.

Example in C++ | With Arguments

Naturally console commands can support arguments, to do so there is only a few changes needed, the function mainly as to have a const TArray<FString>& paramater:

// CPP
#include "ConsoleCheats.h"

static FAutoConsoleCommand CustomCommand
(
    /*Name*/TEXT("c.CustomCommand"),
    /*Info*/TEXT("A custom command that prints hello world!"),
    /*Func*/FConsoleCommadWithArgsDelegate::CreateLambda([] (const TArray<FString>& Args) { UE_LOG(LogTemp, Display, *Args[0]) })
);

void UConsoleCheats::InitCheatManager()
{
   ReceiveInitCheatManager();
}