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"

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

than

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

image

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

cpp

void UOverrideR::RunExec(UObject* passedObj, FString Command)
{
	if(passedObj!=nullptr){
		UWorld* w = passedObj->GetWorld();
		if(w!=nullptr){
			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)

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

.h

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

.cpp

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

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.