# How to run any console command in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509>\
**Category:** C++\
**Tags:** unreal-engine, Rendering, packaging-projects, Blueprint, CPP\
**Created:** [November 27, 2022, 9:30pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509 "2022-11-27T21:30:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheSubZer0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheSubZer0](https://forums.unrealengine.com/u/TheSubZer0)\
**Post date:** [November 27, 2022, 9:30pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509/1 "2022-11-27T21:30:19Z")

</div>

```auto
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "OverrideR.generated.h"

/**
 * 
 */
UCLASS()
class MYPROJECT_API UOverrideR : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
		
		UFUNCTION(BlueprintCallable, Category = "Rendering")
		static void RunExec(FString Command);
};

```

than

```auto
// Fill out your copyright notice in the Description page of Project Settings.

#include "OverrideR.h"

void UOverrideR::RunExec(FString Command)
{
	/* GEngine->Exec(GetWorld(), TEXT("%s"), *Command); <<< "*Command" return invalid */

	GEngine->Exec(GetWorld(), TEXT("r.BoloomQuality=0"));

}

```

This is a macro that is not able to get the World to run console commands.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 27, 2022, 9:43pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509/2 "2022-11-27T21:43:49Z")

</div>

> [@Run console command via C++?](https://forums.unrealengine.com/t/run-console-command-via-c/3942/2):
>
> Though we generally try to avoid writing code that relies on executing console commands, you can do it with: UWorld::Exec(…) E.g,: GetWorld()-\>Exec(GetWorld(), TEXT(“MyAwesomeConsoleCommand X Y Z”));

---

<div class="post-metadata">

**Author:** ![TheSubZer0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheSubZer0](https://forums.unrealengine.com/u/TheSubZer0)\
**Post date:** [November 27, 2022, 9:57pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509/3 "2022-11-27T21:57:15Z")

</div>

cant run it from macro, just CPP controller or character class…

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/e/5/5e5c9b9d42644d8b9c7c49553f77ddec74697e0c.png)

```auto
	GetWorld()->Exec(GetWorld(), *Command);

```

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 27, 2022, 10:27pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509/4 "2022-11-27T22:27:17Z")

</div>

> [@TheSubZer0](#):
>
> `UBlueprintFunctionLibrary`

Oh yeah you are using it in a library so it’s static. You need to pass in a world context object in the static function (just pass in a UObject reference) and get the world from the passed object.

```auto
		static void RunExec(UObject* passedObj, FString Command);

```

cpp

```auto
void UOverrideR::RunExec(UObject* passedObj, FString Command)
{
	if(passedObj!=nullptr){
		UWorld* w = passedObj->GetWorld();
		if(w!=nullptr){
			w->Exec(w, *Command);
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![TheSubZer0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheSubZer0](https://forums.unrealengine.com/u/TheSubZer0)\
**Post date:** [November 27, 2022, 10:38pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509/5 "2022-11-27T22:38:16Z")

</div>

thank you very much for your help, just a doubt what exactly would the object be? Level name or class controller? Sorry for the bad question, because I’m not a programmer in C++, only in C# and “Blueprints”.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 27, 2022, 10:39pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509/6 "2022-11-27T22:39:22Z")

</div>

it can be anything that inherits from uobject (so nearly any basic class in unreal as it’s the base class)

p.s.  
you have a typo in your command. You wrote BoloomQuality instead of BloomQuality  
try it without the equals sign:  
r.BloomQuality 0  
r.BloomQuality 1

Also seems like calling it on basic uobjects doesn’t always work, but pushing it to the player controller seems to work a 100% of the time

```auto
.h

UFUNCTION(BlueprintCallable)
		static void RunExec(APlayerController* TargetPC, FString Command);

```

.cpp

```auto
void UOverrideR::RunExec(APlayerController* TargetPC, FString Command)
{
	TargetPC->ConsoleCommand(Command, true);
}

```

---

<div class="post-metadata">

**Author:** ![TheSubZer0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheSubZer0](https://forums.unrealengine.com/u/TheSubZer0)\
**Post date:** [November 27, 2022, 11:04pm UTC](https://forums.unrealengine.com/t/how-to-run-any-console-command-in-c/716509/7 "2022-11-27T23:04:56Z")

</div>

Yes, it worked, I was using r.BloomQuality=0 and the terminal was saying that the command was unknown, I thought that the default “Execute Console Command” from ue4 Blueprints did not accept certain commands and so I decided to create a custom function in C++, but I was wrong. Thanks a lot for the help. If you want to know the context of me wanting this is in this topic kkkk: [Brute force override rendering settings.](https://forums.unrealengine.com/t/brute-force-override-rendering-settings/716033)
