Why C++

I feel pretty comfortable in C++ and I want to have great performance in the games I make so, for me, using C++ while developing in Unreal Engine 4 was the way to go.

I’m very impressed with what can be accomplished by using Blueprints alone: you have almost full access to everything in the engine. At the same time, I’m scared by what the graphs will look like as complexity increases.

Finally, if a game is just a light wrapper over the functionality the engine provides, then Blueprints will perform just fine. On the other hand, if you want to write code with enough computational cost (in my case AI) then I would expect Blueprints to be as much as 10x slower than C++ (though performance will be much closer with Nativization).

Setting Up

While you don’t need anything additional if you just use Blueprints, in order to use C++ you’ll need to install Visual Studio. As of Unreal 4.23 I installed Visual Studio 2019 and it works fine. Detailed instructions are here, so I’ll just mention a few points of interest:

  • Make sure to install the debug symbols when you install Unreal. Being able to step inside Unreal‘s code when debugging is invaluable. If you have already installed it and you forgot, follow these steps:
    1. In the Epic Launcher click the arrow and select Options:
    2. Select Editor symbols for debugging
  • Update the toolbar:
    • Resize the Configurations drop-down menu. Go to Tools/Customize, select the Commands tab and then select Toolbar and Standard, click on Solution Configuration and press the Modify Selection button, then enter Width 150:

      Before:
      After:
    • Add the Disconnect button to the Debug toolbar. Again in Tools/Customize, select Toolbar and Debug, then add the Disconnect button. This will be useful when we’re debugging (see below):

It’s very important that when you’re coding, you’re getting immediate feedback if there is a bug in your code, even before you compile. While many people enjoy using Visual Assist or Resharper, I have found IntelliSense (which comes with Visual Studio) to be generally serviceable, with the occasional glitch. See here for instructions in case it doesn’t work out of the box. I like having a shortcut to rescan the file if it gets stuck with a false positive; go to Tools/Options, select Keyboard and configure a hotkey (I use Ctrl+Shift+R):

You should be ready now to start writing C++ in Unreal Engine.

Build Times

My initial experience with build times on my 5-year old Intel Core i5 with 16GB of RAM wasn’t encouraging. I was able to determine that simply running cl.exe (this is Visual Studio‘s C++ compiler) sometimes would block for along time – my theory was that it needed a large amount of memory to process both IntelliSense and all the precompiled headers, and even with 16GB it wasn’t quite enough.

So I followed this guide and was able to get compile times down to around 10-20 seconds most of the time. Not perfect but definitely manageable. These are the areas that helped the most.

Disabling unity builds

This improved performance the most, with unity builds each call to cl.exe is much more expensive, typically causing stalls of up to 2-3 minutes.

In the folder C:\Users\<USERNAME>\AppData\Roaming\Unreal Engine\UnrealBuildTool create a file called BuildConfiguration.xml

Contents of the file:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration xmlns="https://www.unrealengine.com/BuildConfiguration">
	<BuildConfiguration>
		<bUseUnityBuild>false</bUseUnityBuild>
	</BuildConfiguration>
</Configuration>

Intermediate folder in an SSD drive

My computer has two hard drives, one smaller SSD drive and one larger 7200 rpm regular drive. I prefer to put my projects in the larger drive, as they can take up to several GB, depending on the assets. However, the performance is not the best.

When I installed Unreal Engine I already placed it in the SSD drive, so I was already set on that regard.

The output of the compilation goes into a folder called Intermediate, under the project folder. Ideally, we want that to be in the SSD drive, which is much faster, especially when writing. The easiest way to accomplish that is by creating a symbolic link or a junction.

See here for instructions on how to do that.

Workflow and Debugging

I believe stepping over your code in the debugger is an essential step to ensure it is working as intended: just seeing the results in the game window is not enough.

After some experimentation, this is the workflow I follow when writing code:

  1. Open Visual Studio, select the DebugGame Editor configuration and launch Unreal from there by pressing F5.
  2. If I’m mostly working in the editor (e.g. editing Blueprints or Data Tables), I can disconnect the debugger (using the toolbar button we added earlier) and keep Visual Studio open in case a quick change is necessary.
  3. If I’m mostly writing C++ code, I’ll keep the debugger connected. I didn’t have much luck reattaching to the process, debugging wouldn’t work.
  4. While the debugger is running, you can’t build from within Visual Studio so I just click the Compile button in the editor instead. I have found it’s slower to compile from the editor, which doesn’t make a lot of sense to me, but the extra delay is tolerable.
  5. In either case, Unreal will reload the changes. I have found this process to fail occasionally and produce unexpected results. In that case I simply close the editor and start it again.

Note that when using this configuration only your game code will be in Debug mode, so performance would still be good and you should see any debugging weirdness. Also, if you installed debug symbols you can step into any Unreal function to see the implementation in detail.

Because I have no plans to modify the engine I didn’t see the need to recompile it.

I haven’t really tried Live Coding because even with regular Hot Loading I already saw some weirdness and I’m a bit wary of solutions that only work some of the time.

What You Should Know

Even if you already know C++, Unreal‘s flavour has a number of peculiarities. I highly recommend reading over these pages:

  • Epic‘s Coding Standard
  • Unreal Engine‘s Architecture. We’ll talk about the specific architecture of my game in a future post.
  • You need to know about UObject‘s and how to create your own
  • You will need to expose functionality to Blueprints. Here’s how.