skip to Main Content

Adding a C++ Class

In this tutorial I am going to

  1. Show you how to add a C++ class (which will have no behavior at all – just for this tutorial).
  2. Explain the difference between source and header files.
  3. 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:

  1. Click the File dropdown menu in the upper-left corner of the UE4 Editor
  2. Select New C++ Class
  3. In the Choose Parent Class menu choose the Actor option and click Next. *
    ParentClass
    Choosing the Parent Class
  4. 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.
  5. Click Create Class
    name_field
    Choosing the name of the C++ 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:

vs0

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): 

 vs

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). 

HeaderSource

 

  • 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:

  1. Click on the Build dropdown menu
  2. Click the Build MyProject as shown below:

vs1

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:

  1. Click on the Compile button which is located at the top center bar of the editor editor_compile

 

Avatar photo

This Post Has One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top