UE4 Tips and Tricks

A grab bag of quick tips and tricks covering niche problems and frustrations. Such as fixing C++ project structure after adding new classes in the public/private sub folder.

Fixing C++ Public and Private Project Structure

In this quick tutorial we're going to fix the public private c++ folder structure when you have just created a new project and have added a new c++ class. Template projects created by the Unreal Engine editor don't automatically organize the .cpp and .h files into private and public folders. We'll follow Epic Game's best practices which is shown in the Learning Action RPG sample project that utilizes a public private folder structure for all c++ source files. When we create a new C++ class in our project and we assign it to the public/private folders it will create a new subfolder hierarchy that will make it difficult to `#include "file.h"` our new file. Lets start by making a new project from our First Person Shooter template within the Unreal Engine editor. Ensure we select C++ and we'll name this our CPPTestPrj. We have our basic first person shooter template. We can play it and see we have all the c++ starter content. Go into our C++ classes folder. Create a new C++ class. For this example we'll create a basic TestActor and set it to public. Next we can see under our `source` folder. We have the initial `.cpp` and `.h` files and we also have our Private and Public folders with the `TestActor.cpp` and `TestActor.h` files. If we wanted to pull in our TestActor class. We can open our Projectile class and add our TestActor as a UPROPERTY with forward look ahead. We add a Test category to the UPROPERTY macro and we'll remove the meta for our simple use case. Back in our `Projectile.cpp` file and we try to `#include "TestActor.h"` we'll get an error as the file path is actually `public/TestActor.h` which we might not expect if we're new to UE4 and C++. Next we'll close out of our Visual Studio project. Go into our C++ classes folder in the content browser. Right click and go to `Show in Explorer`. We can see that all the `.cpp` files and all the `.h` files are outside of the Private and Public folder structure. We'll move the `.cpp` files into our private folder and all the `.h` files into our public folder. Next, go to `File > Refresh Visual Studio Project`. Once the Visual Studio project has been refreshed which properly rebuilds the files with the correct paths we can open the project within Visual Studio. `File > Open Visual Studio`. With a refresh of our project we can see that our `.cpp` files are within our private directory and our `.h` files are within our public directory. Reopen our Projectile class and we no longer see an error for our `#include "TestActor.h"` file which shows the paths are now correct for our game classes. Initiate a project build with `F7` or `Build > Build Solution` dropdown. With this final rebuild of our project we can see our build succeeds and we have successfully fixed our c++ public private folder hierarchy. Following this folder structure is part of the best practices utilized by Epic games and can be found in the Learning Action RPG sample game from Epic Games. I hope you found this useful for following the best practices laid out by Epic games and helps you organize your C++ code base and helps bring much needed standardized structure to your projects. Happy coding!