Create A Standalone Application in UE4

Although UE is a game engine, it is not limited to writing games—you can even use it to write Win32 GUI applications 😏. Typically, we create a UE game project using the Editor and build our own classes based on it for use in the game. However, if you don’t want to create a game project, UE also supports Standalone Applications that can construct their own programs autonomously from the main function, fully controlling which Modules to enable without relying on the engine’s own logical architecture. This can also serve as a lightweight method for learning and testing UE modules.

Note: UE does not provide a direct method to create a Standalone Application, so I wrote a tool to create a Program project: hxhb/ue4program, and implemented a demo for a standalone tool: hxhb/UE4Launcher.

Forward

The content of this article requires using the engine built from source locally (source version engine) rather than the engine installed via EpicGameLauncher (installed version engine). In my previous articles, I introduced that UBT checks for the existence of the Engine/Build/InstalledBuild.txt file to determine if the engine is installed from the Launcher or compiled from source. However, there are more differences between the source version and the installed version; some ThirdParty build.cs files differ, leading to different compilation behaviors for the same code in source and installed version engines (for example, Engine/Source/ThirdParty/HarfBuzz). Although these issues can be resolved, I do not recommend you do so.

Create Program

First, download my written hxhb/ue4program and add it to your system PATH. The usage after adding is as follows:

1
2
# ue4program.exe ProgramName
$ ue4program.exe StandaloneApplication

At this point, a StandaloneApplication folder will be created in the current directory, with the following directory structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
StandaloneApplication
│ GenerateProgramProject.bat
│ OpenProgramProject.bat
│ StandaloneApplication.Build.cs
│ StandaloneApplication.Target.cs

├─Resources
│ Icon.ico
│ Resource.h
│ Resource.rc
│ VersionResource.inl

└─Source
├─Private
│ │ RealExecutionMain.cpp
│ │ StandaloneApplication.cpp
│ │
│ ├─Console
│ │ ConsoleMain.cpp
│ │
│ └─Windows
│ WindowsMain.cpp

└─Public
RealExecutionMain.h
StandaloneApplication.h
StandaloneApplicationLog.h

This article focuses on analyzing the *.target.cs and *.build.cs files. After creating it, move the StandaloneApplication folder to Engine\Source\Programs, as all UE auxiliary programs (UHT/UBT, etc.) are under this directory. Then, run GenerateProgramProject.bat; this is a script I wrote based on UE’s GenerateProjectFiles.bat, which calls UBT to create the Standalone Application program, with the command as follows:

1
2
# bash path in Engine\Source\Programs\StandaloneApplication
$ UnrealBuildTool.exe -notinstallengine -ProjectFiles StandaloneApplication

Note: Using the command above directly in the installed version of the engine will result in an error (creating non-Game projects is not allowed) because even though the -notinstallengine parameter is passed, the detection priority for the Engine/Build/InstalledBuild.txt file is higher than -notinstallengine:

1
2
3
Zhalipeng MINGW64 /d/UE_4.18/Engine/Source/Programs/StandaloneApplication (master)
$ ../../../../Engine/Binaries/DotNET/UnrealBuildTool.exe -notinstallengine -ProjectFiles StandaloneApplication
ERROR: UnrealBuildTool Exception: A game project path was not specified, which is required when generating project files using an installed build or passing -game on the command line

My script includes a check that, if the installed version of the engine is used, will first rename InstalledBuild.txt, and restore it after production (but I still recommend not executing this in the installed version engine). Once executed, it will generate the following in Engine\Intermediate\ProjectFiles:

1
2
3
StandaloneApplication.vcxproj
StandaloneApplication.vcxproj.filters
StandaloneApplication.vcxproj.user

This essentially adds the current project to the UE solution, and after execution, you can open the UE4.sln file in the engine root directory to see the created project:

First, let’s compile and run to see the results:

Program *.target.cs

UBT supports several Target types (specified by the enum type TargetType):

  • Game: Independently running game;
  • Client: Same as Game, but does not contain any server code;
  • Server: Same as Game, but does not contain any client code;
  • Editor: Extension of the UE editor;
  • Program: Independently running program;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Engine/Source/Programs/UnrealBuildTool/Configuration/TargetRules.cs
public enum TargetType
{
// Cooked monolithic game executable (GameName.exe). Also used for a game-agnostic engine executable (UE4Game.exe or RocketGame.exe)
Game,
// Uncooked modular editor executable and DLLs (UE4Editor.exe, UE4Editor*.dll, GameName*.dll)
Editor,
// Cooked monolithic game client executable (GameNameClient.exe, but no server code)
Client,
// Cooked monolithic game server executable (GameNameServer.exe, but no client code)
Server,
// Program (standalone program, e.g. ShaderCompileWorker.exe, can be modular or monolithic depending on the program)
Program,
}

For documentation on .target.cs: UnrealBuildTool/Targets.

The generated *.target.cs template from hxhb/ue4program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using UnrealBuildTool;
using System.Collections.Generic;

[SupportedPlatforms(UnrealPlatformClass.All)]
public class StandaloneApplicationTarget : TargetRules
{
public StandaloneApplicationTarget(TargetInfo Target) : base(Target)
{
Type = TargetType.Program;
LinkType = TargetLinkType.Monolithic;
LaunchModuleName = "StandaloneApplication";
ExtraModuleNames.Add("EditorStyle");
}

public override void SetupGlobalEnvironment(
TargetInfo Target,
ref LinkEnvironmentConfiguration OutLinkEnvironmentConfiguration,
ref CPPEnvironmentConfiguration OutCPPEnvironmentConfiguration
)
{
// Lean and mean
bCompileLeanAndMeanUE = true;

// No editor or editor-only data is needed
bBuildEditor = false;

// Whether to compile WITH_EDITORONLY_DATA disabled. Only Windows will use this, other platforms force this to false.
//bBuildWithEditorOnlyData = false;

// Compile out references from Core to the rest of the engine
bCompileAgainstEngine = false;

// Enabled for all builds that include the CoreUObject project. Disabled only when building standalone apps that only link with Core.
bCompileAgainstCoreUObject = true;

// Whether to include plugin support.
bCompileWithPluginSupport = true;

// Enable exceptions for all modules
bForceEnableExceptions = false;

// Enable RTTI for all modules.
// bForceEnableRTTI = true;

// If true the program entrance is WinMain, otherwise the entrance is main
bIsBuildingConsoleApplication = false;
}
}

Currently, the three main parameters to focus on are:

  • Type: Specify as the target type to build TargetType.Program.
  • LinkType: Specify TargetLinkType.Monolithic to compile the target into a single executable file (not relying on any DLL).
  • bIsBuildingConsoleApplication: true supports Console, with the entry point being main; false does not support Console, with the entry point being WinMain.

Other parameters (RTTI/Exceptions) can be queried based on demand when used.

Program *.Build.cs

The *.Build.cs of the Program does not differ from those of ordinary game projects because Game is a module and Program is also one; the only difference is in how these modules are built into target types (determined by *.target.cs).

Demo: UELauncher

hxhb/UE4Launcher is a handy UE Project launcher I wrote using UE’s Program mode, which conveniently allows adding launch parameters and selecting engine versions. You can download and install it from hxhb/UE4Launcher/release. I have a separate article introducing the tool, update logs, and download links: Open Source Unreal Engine Launcher UE Launcher.

Main interface:

Edit configuration files:

Right-click menu association for .uproject files:

Right-click menu association for .uejson files:

Command line parameter support:

  • -g: The -g parameter followed by the .uproject file generates a .uejson configuration file for it;
  • -e: The -e parameter followed by a .uejson file opens the window to edit that configuration file;
  • -c: The -c parameter followed by a .uejson file launches that configuration (without opening the edit window)
1
2
3
4
5
6
# generate LauncherConf_ShooterGame.uejson
$ UE4Launcher.exe -g ShooterGame.uproject
# Launch Edit Window with .uejson
$ UE4Launcher.exe -e LauncherConf_ShooterGame.uejson
# Launch Conf
$ UE4Launcher.exe -c LauncherConf_ShooterGame.uejson

The source code also supports very simple extensions to other UE tools, such as UBT/AutomationTool, which can all be added as tools to start (with Editor/Editor-cmd as the default).

End

Using Slate to write UI can refer to various control usages in the SlateViewer template example and can also draw on the implementation of UnrealFrontend. Of course, Standalone Application can do much more than just writing UI; all modules in UE can be used, making it a super powerful Library, and it is cross-platform. Explore more of its wonderful features gradually.

The article is finished. If you have any questions, please comment and communicate.

Scan the QR code on WeChat and follow me.

Title:Create A Standalone Application in UE4
Author:LIPENGZHA
Publish Date:2019/03/30 18:14
Update Date:2020/03/18 14:10
World Count:4.8k Words
Link:https://en.imzlp.com/posts/31962/
License: CC BY-NC-SA 4.0
Reprinting of the full article is prohibited.
Your donation will encourage me to keep creating!