# How do i run a Console Command in C++

**URL:** <https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988>\
**Category:** C++\
**Tags:** console-command, unreal-engine, bug-report, CPP, question\
**Created:** [May 9, 2015, 12:06pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988 "2015-05-09T12:06:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_adc3f730](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_adc3f730](https://forums.unrealengine.com/u/anonymous_user_adc3f730)\
**Post date:** [May 9, 2015, 12:06pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/1 "2015-05-09T12:06:35Z")

</div>

Hi, i saw [this thread](https://forums.unrealengine.com/showthread.php?4419-Run-console-command-via-C) 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 😕 it has to be direct like in the thread above. Any Ideas?

---

<div class="post-metadata">

**Author:** ![Evols](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Evols](https://forums.unrealengine.com/u/Evols)\
**Post date:** [May 9, 2015, 7:35pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/2 "2015-05-09T19:35:41Z")

</div>

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](http://en.cppreference.com/w/cpp/language/string_literal). Example: TEXT(“UE4”) will become L"UE4".

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8bf3f3f2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8bf3f3f2](https://forums.unrealengine.com/u/anonymous_user_8bf3f3f2)\
**Post date:** [November 15, 2016, 7:18pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/3 "2016-11-15T19:18:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steve1222](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@steve1222](https://forums.unrealengine.com/u/steve1222)\
**Post date:** [January 14, 2019, 6:17pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/4 "2019-01-14T18:17:23Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Steazy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steazy](https://forums.unrealengine.com/u/Steazy)\
**Post date:** [July 8, 2019, 10:12pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/5 "2019-07-08T22:12:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steazy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steazy](https://forums.unrealengine.com/u/Steazy)\
**Post date:** [July 8, 2019, 10:15pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/6 "2019-07-08T22:15:08Z")

</div>

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" ) );
```

---

<div class="post-metadata">

**Author:** ![Lukalake](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lukalake](https://forums.unrealengine.com/u/Lukalake)\
**Post date:** [August 1, 2022, 3:05pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/7 "2022-08-01T15:05:29Z")

</div>

Actually I guess this can work by adding something more.

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Aviopene](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/aviopene/32/720968_2.png) [@Aviopene](https://forums.unrealengine.com/u/Aviopene)\
**Post date:** [August 8, 2023, 3:35am UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/8 "2023-08-08T03:35:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swrcVendetta](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@swrcVendetta](https://forums.unrealengine.com/u/swrcVendetta)\
**Post date:** [January 6, 2025, 2:52pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/9 "2025-01-06T14:52:16Z")

</div>

Why is this not working:

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

```

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

---

<div class="post-metadata">

**Author:** ![RogueSheep](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roguesheep/32/891970_2.png) [@RogueSheep](https://forums.unrealengine.com/u/RogueSheep)\
**Post date:** [January 19, 2026, 2:35pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/11 "2026-01-19T14:35:59Z")

</div>

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

Works for me (in 5.5)

```

---

<div class="post-metadata">

**Author:** ![LuckyPython9902](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LuckyPython9902](https://forums.unrealengine.com/u/LuckyPython9902)\
**Post date:** [August 13, 2026, 3:33pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/12 "2026-08-13T15:33:58Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![SolarflareSoft](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/solarflaresoft/32/1192396_2.png) [@SolarflareSoft](https://forums.unrealengine.com/u/SolarflareSoft)\
**Post date:** [August 13, 2026, 5:56pm UTC](https://forums.unrealengine.com/t/how-do-i-run-a-console-command-in-c/317988/13 "2026-08-13T17:56:42Z")

</div>

(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:

```cpp
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.
