How do i run a Console Command in C++

Hi, i saw this thread on how to run a console command via c++, but i want to do something like this.

FString MyCommandString = "r.SetRes ";
FString Resolution = "1920x1080";

FString Final = MyCommandString + Resolution;

GetWorld()->Exec(GetWorld(), TEXT(Final));

but i get horrible errors like ‘LFinal is undefined’ ? where does the ‘L’ come from?
I can’t Execute a var string command :confused: it has to be direct like in the thread above. Any Ideas?

Exec() takes in parameter a const TCHAR*. But, TEXT(SomeString) does not perform FString conversion to const TCHAR*. To do this, you have to use the * operator. So, try this instead:

GetWorld()->Exec(GetWorld(), *Final);

TEXT(“some text”) should only be used to make a const TCHAR* for litterals (i.e. defining a string with quotes). Final is a FString, not a litteral. At a lower level, TEXT() adds a L before the litteral, to tell the compiler that the string is in wide char, as described in this page. Example: TEXT(“UE4”) will become L"UE4".

GetOwningPlayer()->ConsoleCommand(“StringValue”);

Thnx for that carlitalica. I think exec isnt working because it is expecting a single character. So even when passing it a string its only using the first character. But I havn’t found any more information on UWorld::Exec() yet…

As far as I can tell UWorld::Exec only handles a subset of possible console commands… a very small subset. It seems that APlayerController::ConsoleCommand is the closest to actually typing it into the console. UEngine (GEngine) also has an Exec command that covers a larger set of commands, including forwarding to UWorld::Exec.

So I would argue this answer is misleading and effectively wrong with regard to the question. Though correct with regards to proper use of the TEXT("") macro.

I would recommend using APlayerController::ConsoleCommand if you can easily get the appropriate APlayerController reference.

Using UEngine::Exec via GEngine does seem to cover a large subset of console commands (but not all, as some will need a target and must be routed through the PlayerController in order to find said target).

GEngine->Exec( GetWorld(), TEXT( "stat startfile" ) );

Actually I guess this can work by adding something more.

FProcHandle Proc = FPlatformProcess::CreateProc(*CodeLitePath, nullptr, true, false, false, nullptr, 0, yourworkingdirectory(this must not be empty), nullptr);
	if(Proc.IsValid())
	{
		FPlatformProcess::CloseProc(Proc);
		return true;
	}
	return false;

I can confirm that PlayerController->ConsoleCommand(*cmd) works perfectly

Why is this not working:

    FString cmd = "Stat UNIT";
    GetWorld()->Exec(GetWorld(), *cmd);

I am doing this in BeginPlay on my GameModeBase in UE5.4

APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
FString Res = PlayerController->ConsoleCommand(*Cmd, false);

Works for me (in 5.5)

It’s because World::Exec only recognise a limited set of command.

// Handle Exec/Console Commands related to the World
e.g. TRACETAG, TRACETAGALL, FLUSHPERSISTENTDEBUGLINES, …

You probably want APlayerController::ConsoleCommand instead or UKismetSystemLibrary::ExecuteConsoleCommand

(This post is a bit long, but you can jump to the end for a quick summary.)

Anything involving an Exec function anywhere (other than in GEngine) is bound to fail. As I understand it, these Exec functions are only the implementations of console commands - when a console command is executed, it’s passed to the Exec function of all the console handlers in scope in turn, until one of them reports that it was handled.

If you look at the KismetSystemLibrary, you can see that it requires a player controller is required to execute a console command, so that’s probably the easiest way if you just have a specific command you want to execute (you can also do it through the UPlayer, which is 100% equivalent). However, this might miss commands defined in the game instance or game viewport; I might be missing something, but I can’t see where the player controller’s exec routes into the game viewport’s exec, though a comment in the source code suggests that this link does exist somewhere.

From what I can figure out, the real entry point to the console subsystem is through the Game Viewport. Although passing the command through the player controller may be easier in many cases, if you want something that will understand any known command (for example, if you’re building a custom UI for the console), I’d suggest trying something like this approach:

bool ExecuteConsoleCommand(const FString& cmd) {
    if(auto instance = GetWorld()->GetGameInstance()) {
        if(auto viewport = instance->GetGameViewportClient()) {
            if(auto console = viewport->ViewportConsole) {
                console->ConsoleCommand(cmd);
                return true;
            }
        }
    }
    return false;
}

This will route the command through the most appropriate player controller if one can be found; otherwise, it routes it through the game viewport.

In short: Use PlayerController->ConsoleCommand in most cases (eg when you’re executing a fixed command). Use GameViewportClient->ViewportConsole->ConsoleCommand if you’re trying to replicate the console in a custom widget.