*The content of this article is applicable to UE4.
"I want to add a C++ file (C++ class) in Unreal Engine."
"I was able to add files, but I don’t know how to remove them."
To solve this problem, this article explains how to add/delete C++ header files and source files(classes) in Unreal Engine.
Note: In order to use C++ in Unreal Engine, you need to install Visual Studio 2019. If you have not setup the environment, please refer to the following article.
How to Add/Delete C++ Files (C++ Classes)
When you create C++ classes in Unreal Engine ("UE"), you need to create C++ header file and source file ("C++ file") which contains the source code.
There are two ways to add C++ files.
If you want to delete C++ files, you need to follow method (2).
- Method (1): Add from the UE editor.
- Method (2): Add/delete from Explorer.
Method (1): Add from the UE editor
Open the UE project file (.uproject file) and Launch UE Editor.
Select "Tools" tab – "New C++ Class…", and select the class you want to add.
Then click the "Next" button (See below figure (1) to (4)).
Next, enter the file name (class name) and the save location (See below figure (1) to (2)), and click the "Create Class" button (See below figure (3)).
A C++ file is generated at the specified location.
Return to the folder where the UE project file is located, open the Visual Studio solution file (.sln file) and start the Visual Stduio editor.
The C++ file has been added to the Explorer.
Note that it automatically generates the minimum source code to define the C++ class.
Also, you can find the file name is same as class name.
Build (compile) the software (See below figure (1) to (2)).
If the process ends normally, the compilation is complete.
Now, you can use the created C++ class on the UE editor.
Note that this procedure can add C++ files (classes), but can’t delete them.
Also, you can’t move or rename C++ files.
If you want to perform such operations, you must follow the Method (2).
Method (2): Add/delete from Explorer
Place the C++ files (.h and .cpp files) in the "Source" folder – (project name) folder in the folder where the UE project files (.uproject files) are located.
It is also possible to place them in the child folders.
Note: On this phase, you can add any data (source code) in the C++ files.
Return to the folder where the UE project files are located.
Then, right-click on the UE project file (.uproject file) and select "Generate Visual Studio project files".
Note: If there is not a menu item "Generate Visual Studio project files", please refer to the following article**.
Open the Visual Studio solution file (.sln file) and launch the Visual Stduio editor.
The C++ file is added on the Explorer.
Note that the data (source code) in the C++ file remains as it was when the file was placed.
Unlike Method (1), you need to write the source code yourself from scratch.
After you write the source code, build (compile) them (See below figure (1) to (2)).
If the process ends normally, the compilation is finished.
You can use the C++ class on the UE editor.
Note that the .sln file will be up-to-date after you execute the menu item "Generate Visual Studio project files" on the .uproject file.
All changes on the C++ files will be reflected.
Summary (Pros/Cons of Each Method)
Pros/Cons of each method are as follows.
UE C++ has a unique description that is not found in regular C++, so you will face with the compilation errors.
Therefore, we recommend you to create classes by using method (1).
Method (1) Pros/Cons of Adding from the UE Editor
- You can only add C++ files.
- Generate minimum source code automatically. It allows you to start creating classes immediately.
Method (2) Pros/Cons of Editing on the Explorer
- You can add/delete/move/rename) of C++ files.
- You need to write source code from scratch by yourself. You sometime need to copy other source code.