Unreal Engine 4 C++ Tutorial

In this Unreal Engine 4 tutorial, you will learn how to create C++ classes and expose variables and functions to the editor. By Tommy Tran.

Leave a rating/review
Save for later
Share

Blueprints is a very popular way to create gameplay in Unreal Engine 4. However, if you’re a long-time programmer and prefer sticking to code, C++ is for you! Using C++, you can also make changes to the engine and also make your own plugins.

In this tutorial, you will learn how to:

  • Create C++ classes
  • Add components and make them visible to Blueprints
  • Create a Blueprint class based on a C++ class
  • Add variables and make them editable in Blueprints
  • Bind axis and action mappings to functions
  • Override C++ functions in Blueprints
  • Bind an overlap event to a function

Please note that this is not a tutorial on learning C++. Instead, this tutorial will focus on working with C++ in the context of Unreal Engine.

Note: This tutorial assumes you already know the basics of using Unreal Engine. If you are new to Unreal Engine, you should go through our 10-part Unreal Engine for Beginners tutorial series first.

Getting Started

If you haven’t already, you will need to install Visual Studio. Follow Epic’s official guide on setting up Visual Studio for Unreal Engine 4. (Although you can use alternative IDEs, this tutorial will use Visual Studio as Unreal is already designed to work with it.)

Afterwards, download the starter project and unzip it. Navigate to the project folder and open CoinCollector.uproject. If it asks you to rebuild modules, click Yes.

Unreal Engine 4 C++ Tutorial

Once that is done, you will see the following scene:

Unreal Engine 4 C++ Tutorial

In this tutorial, you will create a ball that the player will control to collect coins. In previous tutorials, you have been creating player-controlled characters using Blueprints. For this tutorial, you will create one using C++.

Creating a C++ Class

To create a C++ class, go to the Content Browser and select Add New\New C++ Class.

Unreal Engine 4 C++ Tutorial

This will bring up the C++ Class Wizard. First, you need to select which class to inherit from. Since the class needs to be player-controlled, you will need a Pawn. Select Pawn and click Next.

Unreal Engine 4 C++ Tutorial

In the next screen, you can specify the name and path for your .h and .cpp files. Change Name to BasePlayer and then click Create Class.

Unreal Engine 4 C++ Tutorial

This will create your files and then compile your project. After compiling, Unreal will open Visual Studio. If BasePlayer.cpp and BasePlayer.h are not open, go to the Solution Explorer and open them. You can find them under Games\CoinCollector\Source\CoinCollector.

Unreal Engine 4 C++ Tutorial

Before we move on, you should know about Unreal’s reflection system. This system powers various parts of the engine such as the Details panel and garbage collection. When you create a class using the C++ Class Wizard, Unreal will put three lines into your header:

  1. #include "TypeName.generated.h"
  2. UCLASS()
  3. GENERATED_BODY()

Unreal requires these lines in order for a class to be visible to the reflection system. If this sounds confusing, don’t worry. Just know that the reflection system will allow you to do things such as expose functions and variables to Blueprints and the editor.

You’ll also notice that your class is named ABasePlayer instead of BasePlayer. When creating an actor-type class, Unreal will prefix the class name with A (for actor). The reflection system requires classes to have the appropriate prefixes in order to work. You can read about the other prefixes in Epic’s Coding Standard.

Note: Prefixes will not display within the editor. For example, if you wanted to create a variable of type ABasePlayer, you would search for BasePlayer.

That’s all you need to know about the reflection system for now. Next, you will add a player model and camera. To do this, you need to use components.

Adding Components

For the player Pawn, you will add three components:

  1. Static Mesh: This will allow you to select a mesh to represent the player
  2. Spring Arm: This component operates like a camera boom. One end will be attached to the mesh and the camera will be attached to the other end.
  3. Camera: Whatever this camera sees is what Unreal will display to the player

First, you need to include headers for each type of component. Open BasePlayer.h and then add the following lines above #include "BasePlayer.generated.h":

#include "Components/StaticMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

If it is not the last include, you will get an error when compiling.

Note: It is important that the .generated.h file is the last include. In this case, your includes should look like this:
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "BasePlayer.generated.h"

If it is not the last include, you will get an error when compiling.

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "BasePlayer.generated.h"

Now you need to declare variables for each component. Add the following lines after SetupPlayerInputComponent():

UStaticMeshComponent* Mesh;
USpringArmComponent* SpringArm;
UCameraComponent* Camera;

The name you use here will be the name of the component in the editor. In this case, the components will display as Mesh, SpringArm and Camera.

Next, you need to make each variable visible to the reflection system. To do this, add UPROPERTY() above each variable. Your code should now look like this:

UPROPERTY()
UStaticMeshComponent* Mesh;

UPROPERTY()
USpringArmComponent* SpringArm;

UPROPERTY()
UCameraComponent* Camera;

You can also add specifiers to UPROPERTY(). These will control how the variable behaves with various aspects of the engine.

Add VisibleAnywhere and BlueprintReadOnly inside the brackets for each UPROPERTY(). Separate each specifier with a comma.

UPROPERTY(VisibleAnywhere, BlueprintReadOnly)

VisibleAnywhere will allow each component to be visible within the editor (including Blueprints).

BlueprintReadOnly will allow you to get a reference to the component using Blueprint nodes. However, it will not allow you to set the component. It is important for components to be read-only because their variables are pointers. You do not want to allow users to set this otherwise they could point to a random location in memory. Note that BlueprintReadOnly will still allow you to set variables inside of the component, which is the desired behavior.

Note: For non-pointer variables (int, float, boolean etc.), use EditAnywhere and BlueprintReadWrite instead.

Now that you have variables for each component, you need to initialize them. To do this, you must create them within the constructor.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.