How to run any console command in C++?

// 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"

class MYPROJECT_API UOverrideR : public UBlueprintFunctionLibrary
		UFUNCTION(BlueprintCallable, Category = "Rendering")
		static void RunExec(FString Command);


// 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.

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


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

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.

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


void UOverrideR::RunExec(UObject* passedObj, FString Command)
		UWorld* w = passedObj->GetWorld();
			w->Exec(w, *Command);
1 Like

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”.

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

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


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


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

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.