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
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Creating the Jump Function

First you need to bind the jump mapping to a function. In this tutorial, jump is set to space bar.

Unreal Engine 4 C++ Tutorial

Go back to Visual Studio and open BasePlayer.h. Add the following below MoveRight():

UPROPERTY(EditAnywhere, BlueprintReadWrite)
float JumpImpulse;

UFUNCTION(BlueprintImplementableEvent)
void Jump();

First is a float variable called JumpImpulse. You will use this when implementing the jump. It uses EditAnywhere to make it editable within the editor. It also uses BlueprintReadWrite so you can read and write it using Blueprint nodes.

Next is the jump function. UFUNCTION() will make Jump() visible to the reflection system. BlueprintImplementableEvent will allow Blueprints to implement Jump(). If there is no implementation, any calls to Jump() will do nothing.

Note: If you want to provide a default implementation in C++, use BlueprintNativeEvent instead. You’ll learn how to do this later on in the tutorial.

Since Jump is an action mapping, the method to bind it is slightly different. Close BasePlayer.h and then open BasePlayer.cpp. Add the following inside SetupPlayerInputComponent():

InputComponent->BindAction("Jump", IE_Pressed, this, &ABasePlayer::Jump);

This will bind the Jump mapping to Jump(). It will only execute when you press the jump key. If you want it to execute when the key is released, use IE_Released instead.

Up next is overriding Jump() in Blueprints.

Overriding Functions in Blueprints

Compile and then close BasePlayer.cpp. Afterwards, go back to Unreal Engine and open BP_Player. Go to the My Blueprints panel and hover over Functions to display the Override drop-down. Click it and select Jump. This will create an Event Jump.

Unreal Engine 4 C++ Tutorial

Note: An override will be an event if there is no return type. If there is a return type, it will be a function.

Next, create the following setup:

Unreal Engine 4 C++ Tutorial

This will add an impulse (JumpImpulse) on the Z-axis to Mesh. Note that in this implementation, the player can jump indefinitely.

Next, you need to set the value of JumpImpulse. Click Class Defaults in the Toolbar and then go to the Details panel. Set JumpImpulse to 100000.

Unreal Engine 4 C++ Tutorial

Click Compile and then close BP_Player. Press Play and jump around using space bar.

Unreal Engine 4 C++ Tutorial

In the next section, you will make the coins disappear when the player touches them.

Collecting Coins

To handle overlaps, you need to bind a function to an overlap event. To do this, the function must meet two requirements. The first is that the function must have the UFUNCTION() macro. The second requirement is the function must have the correct signature. In this tutorial, you will use the OnActorBeginOverlap event. This event requires a function to have the following signature:

FunctionName(AActor* OverlappedActor, AActor* OtherActor)

Go back to Visual Studio and open BaseCoin.h. Add the following below PlayCustomDeath():

UFUNCTION()
void OnOverlap(AActor* OverlappedActor, AActor* OtherActor);

After binding, OnOverlap() will execute when the coin overlaps another actor. OverlappedActor will be the coin and OtherActor will be the other actor.

Next, you need to implement OnOverlap().

Implementing Overlaps

Open BaseCoin.cpp and add the following at the end of the file:

void ABaseCoin::OnOverlap(AActor* OverlappedActor, AActor* OtherActor)
{
}

Since you only want to detect overlaps with the player, you need to cast OtherActor to ABasePlayer. Before you do the cast, you need to include the header for ABasePlayer. Add the following below #include "BaseCoin.h":

#include "BasePlayer.h"

Now you need to perform the cast. In Unreal Engine, you can cast like this:

Cast<TypeToCastTo>(ObjectToCast);

If the cast is successful, it will return a pointer to ObjectToCast. If unsuccessful, it will return nullptr. By checking if the result is nullptr, you can determine if the object was of the correct type.

Add the following inside OnOverlap():

if (Cast<ABasePlayer>(OtherActor) != nullptr)
{
	Destroy();
}

Now, when OnOverlap() executes, it will check if OtherActor is of type ABasePlayer. If it is, destroy the coin.

Next, you need to bind OnOverlap().

Binding the Overlap Function

To bind a function to an overlap event, you need to use AddDynamic() on the event. Add the following inside ABaseCoin():

OnActorBeginOverlap.AddDynamic(this, &ABaseCoin::OnOverlap);

This will bind OnOverlap() to the OnActorBeginOverlap event. This event occurs whenever this actor overlaps another actor.

Compile and then go back to Unreal Engine. Press Play and start collecting coins. When you overlap a coin, the coin will destroy itself, causing it to disappear.

Unreal Engine 4 C++ Tutorial

Note: If the coins don’t disappear, try restarting the editor to do a full recompile. Some code changes require a restart in order to work.

In the next section, you will create another overridable C++ function. However, this time you will also create a default implementation. To demonstrate this, you will use OnOverlap().

Creating a Function’s Default Implementation

To make a function with a default implementation, you need to use the BlueprintNativeEvent specifier. Go back to Visual Studio and open BaseCoin.h. Add BlueprintNativeEvent to the UFUNCTION() of OnOverlap():

UFUNCTION(BlueprintNativeEvent)
void OnOverlap(AActor* OverlappedActor, AActor* OtherActor);

To make a function the default implementation, you need to add the _Implementation suffix. Open BaseCoin.cpp and change OnOverlap to OnOverlap_Implementation:

void ABaseCoin::OnOverlap_Implementation(AActor* OverlappedActor, AActor* OtherActor)

Now, if a child Blueprint does not implement OnOverlap(), this implementation will be used instead.

The next step is to implement OnOverlap() in BP_Coin.

Creating the Blueprint Implementation

For the Blueprint implementation, you will call PlayCustomDeath(). This C++ function will increase the coin’s rotation rate. After 0.5 seconds, the coin will destroy itself.

To call a C++ function from Blueprints, you need to use the BlueprintCallable specifier. Close BaseCoin.cpp and then open BaseCoin.h. Add the following above PlayCustomDeath():

UFUNCTION(BlueprintCallable)

Compile and then close Visual Studio. Go back to Unreal Engine and open BP_Coin. Override On Overlap and then create the following setup:

Unreal Engine 4 C++ Tutorial

Now whenever a player overlaps a coin, Play Custom Death will execute.

Click Compile and then close BP_Coin. Press Play and collect some coins to test the new implementation.

unreal engine C++ tutorial

Where to Go From Here?

You can download the completed project here.

As you can see, working with C++ in Unreal Engine is quite easy. Although you’ve accomplished a lot so far with C++, there is still a lot to learn! I’d recommend checking out Epic’s tutorial series on creating a top-down shooter using C++.

If you’re new to Unreal Engine, check out our 10-part beginner series. This series will take you through various systems such as Blueprints, Materials and Particle Systems.

If there’s a topic you’d like me to cover, let me know in the comments below!

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.