In this tutorial I am going to
- Show you how to add a C++ class (which will have no behavior at all – just for this tutorial).
- Explain the difference between source and header files.
- Show you how you can compile your code.
It doesn’t matter if your project is C++ or Blueprint based, the way to add a C++ class is the same in all projects.
So, let’s start!
Adding the C++ Class
In order to add a C++ Class in your project:
- Click the File dropdown menu in the upper-left corner of the UE4 Editor
- Select New C++ Class
- In the Choose Parent Class menu choose the Actor option and click Next. *
- In the Name Field type the name of your Actor (ie I will name him MyActor) and leave the path, header and source file in their default state.
- Click Create Class
*When creating a class you will be prompted to choose a Parent Class. The Parent Class provides a basic functionality which is provided by Epic.
A progress bar will appear and the editor will compile the new class you’ve just added. After the editor compiles the new class, Visual Studio will pop up and you will see two open tabs, one will contain the MyActor.cpp and the other one will contain MyActor.h as the following screenshot suggests:
Here I chose Actor as Parent Class. An Actor is basically any object that can be placed into a level. However, I won’t place something in the level in this post.
Unreal Engine has many classes and you usually choose the parent class that best fits your needs depends on what you want to create. For example, if you want to create a new character you might choose Character as parent class.
You can find the class you’ve just added in Visual Studio in the Solution Explorer under the name of your project, in the folder Source, in the sub-folder that has the same name as your project as seen in the screenshot below (I named my project “MyProject” for this example):
The Difference between .cpp and .h file
By now, you must have realized that in your project there are two files with the same name as your class but with different extensions. For each class you will have a .cpp file (also known as source file) and a .h file (also known as header file).
- The header file (.h) is used in order to hold the declarations for headers, variables, functions etc. It defines WHAT
- The source file (.cpp) contains all the code which is based on the header file, and is primarily responsible for the actual game loop. It defines HOW
In the screenshot above you can see that MyActor.cpp contains some code blocks by default. These code blocks are auto-generated by the editor.
Compiling your C++
You can compile your C++ either using the UE4 editor or using Visual Studio.
In order to compile your C++ by using Visual Studio:
- Click on the Build dropdown menu
- Click the Build MyProject as shown below:
Note: The name MyProject is the name of my project, so your option might differ.
In order to compile your C++ by using the UE4 Editor:
- Click on the Compile button which is located at the top center bar of the editor