top of page

Execute OS-level terminal commands from within Unreal Engine

  • Writer: The Unreal Guy
    The Unreal Guy
  • Dec 17, 2024
  • 2 min read

We can execute console commands from the unreal engine editor via the FPlatformProcess::CreateProc function call. Execute commands from the Windows default Command Prompt:

FString CommandToExecute = FString("//C mkdir NewFolder");
FPlatformProcess::CreateProc(TEXT("cmd.exe"), *CommandToExecute, true, false, false, nullptr, 0, nullptr, nullptr);

The above code will automatically find the default install location of cmd on the Windows environment and execute any command that is passed via the CommandToExecute variable.

Unreal Engine Command CMD Shell .exe Execute external .exe files from unreal:

FString ExeFilePath = FString("C:\\Users\\TheUnrealGuy\\Desktop\\Test.exe");
FPlatformProcess::CreateProc(*ExeFilePath, nullptr, true, false, false, nullptr, 0, nullptr, nullptr);

The above code will execute any exe file on the path mentioned in the string. to get the path of the exe:

  1. Select the exe file

  2. Right-click on the exe and click on copy as path

  3. Paste the copied path within the double quotes in the ExeFilePath variable

  4. The variable now should look something like this

FString ExeFilePath = FString("C:\Users\TheUnrealGuy\Desktop\Test.exe");
  1. We need to replace all the single backslashes (\) with double backslashes (\\) for cpp to recognize it as a single backslash (\) is used to specify an escape sequence character in cpp


Similarly, we can also execute such console commands in Linux and Mac os:

FString CommandToExecute = FString("<Command to execute>");
FPlatformProcess::CreateProc(TEXT("/bin/sh"), *CommandToExecute, true, false, false, nullptr, 0, nullptr, nullptr);

For Linux and Mac, we need to change the execution command and instead of cmd we will have to run shell Execute these commands from the blueprint:

  1. Go to the header file of your cpp class and add:

protected:
	UFUNCTION(BlueprintCallable)
	void ExecuteConsoleCommand(const FString& Executable, const FString& CommandToExecute);
  1. Create a declaration for the above function in cpp

void AExampleClassName::ExecuteConsoleCommand(const FString& Executable, const FString& CommandToExecute)
{
	FPlatformProcess::CreateProc(*Executable, *CommandToExecute, true, false, false, nullptr, 0, nullptr, nullptr);
}

After compiling the code you can now right-click and search ExecuteConsoleCommand function in your blueprint and you can execute commands via blueprints. I would recommend creating a plugin or an interface call for easy access to this function

 
 
 

Comments


github.png

Github

LinkedIn

Discord

YouTube

© 2035 The Unreal Guy. Powered and secured by Wix

bottom of page