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

Initializing Components

To create components, you can use CreateDefaultSubobject<Type>("InternalName"). Open BasePlayer.cpp and add the following lines inside ABasePlayer():

Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
SpringArm = CreateDefaultSubobject<USpringArmComponent>("SpringArm");
Camera = CreateDefaultSubobject<UCameraComponent>("Camera");

This will create a component of each type. It will then assign their memory address to the provided variable. The string argument will be the component’s internal name used by the engine (not the display name although they are the same in this case).

Next you need to set up the hierarchy (which component is the root and so on). Add the following after the previous code:

RootComponent = Mesh;
SpringArm->SetupAttachment(Mesh);
Camera->SetupAttachment(SpringArm);

The first line will make Mesh the root component. The second line will attach SpringArm to Mesh. Finally, the third line will attach Camera to SpringArm.

Now that the component code is complete, you need to compile. Perform one of the following methods to compile:

  1. In Visual Studio, select Build\Build Solution
  2. In Unreal Engine, click Compile in the Toolbar

Next, you need to set which mesh to use and the rotation of the spring arm. It is advisable to do this in Blueprints because you do not want to hard-code asset paths in C++. For example, in C++, you would need to do something like this to set a static mesh:

static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshToUse(TEXT("StaticMesh'/Game/MyMesh.MyMesh");
MeshComponent->SetStaticMesh(MeshToUse.Object);

However, in Blueprints, you can just select a mesh from a drop-down list.

Unreal Engine 4 C++ Tutorial

If you were to move the asset to another folder, your Blueprints wouldn’t break. However, in C++, you would have to change every reference to that asset.

To set the mesh and spring arm rotation within Blueprints, you will need to create a Blueprint based on BasePlayer.

Note: It is common practice to create base classes in C++ and then create a Blueprint subclass. This makes it easier for roles such as artists and designers to edit classes.

Subclassing C++ Classes

In Unreal Engine, navigate to the Blueprints folder and create a Blueprint Class. Expand the All Classes section and search for BasePlayer. Select BasePlayer and then click Select.

Unreal Engine 4 C++ Tutorial

Rename it to BP_Player and then open it.

First, you will set the mesh. Select the Mesh component and set its Static Mesh to SM_Sphere.

Unreal Engine 4 C++ Tutorial

Next, you need to set the spring arm’s rotation and length. This will be a top-down game so the camera needs to be above the player.

Select the SpringArm component and set Rotation to (0, -50, 0). This will rotate the spring arm so that the camera points down towards the mesh.

Unreal Engine 4 C++ Tutorial

Since the spring arm is a child of the mesh, it will start spinning when the ball starts spinning.

Unreal Engine 4 C++ Tutorial

To fix this, you need to set the rotation of the spring arm to be absolute. Click the arrow next to Rotation and select World.

Unreal Engine 4 C++ Tutorial

Afterwards, set Target Arm Length to 1000. This will place the camera 1000 units away from the mesh.

Unreal Engine 4 C++ Tutorial

Next, you need to set the Default Pawn Class in order to use your Pawn. Click Compile and then go back to the editor. Open the World Settings and set Default Pawn to BP_Player.

Unreal Engine 4 C++ Tutorial

Press Play to see your Pawn in the game.

Unreal Engine 4 C++ Tutorial

The next step is to add functions so the player can move around.

Implementing Movement

Instead of adding an offset to move around, you will move around using physics! First, you need a variable to indicate how much force to apply to the ball.

Go back to Visual Studio and open BasePlayer.h. Add the following after the component variables:

UPROPERTY(EditAnywhere, BlueprintReadWrite)
float MovementForce;

EditAnywhere allows you to edit MovementForce in the Details panel. BlueprintReadWrite will allow you to set and read MovementForce using Blueprint nodes.

Next, you will create two functions. One for moving up and down and another for moving left and right.

Creating Movement Functions

Add the following function declarations below MovementForce:

void MoveUp(float Value);
void MoveRight(float Value);

Later on, you will bind axis mapppings to these functions. By doing this, axis mappings will be able to pass in their scale (which is why the functions need the float Value parameter).

Note: If you are not familiar with axis mappings and scale, check out our Blueprints tutorial.

Now, you need to create an implementation for each function. Open BasePlayer.cpp and add the following at the end of the file:

void ABasePlayer::MoveUp(float Value)
{
	FVector ForceToAdd = FVector(1, 0, 0) * MovementForce * Value;
	Mesh->AddForce(ForceToAdd);
}

void ABasePlayer::MoveRight(float Value)
{
	FVector ForceToAdd = FVector(0, 1, 0) * MovementForce * Value;
	Mesh->AddForce(ForceToAdd);
}

MoveUp() will add a physics force on the X-axis to Mesh. The strength of the force is provided by MovementForce. By multiplying the result by Value (the axis mapping scale), the mesh can move in either the positive or negative directions.

MoveRight() does the same as MoveUp() but on the Y-axis.

Now that the movement functions are complete, you need to bind the axis mappings to them.

Binding Axis Mappings to Functions

For the sake of simplicity, I have already created the axis mappings for you. You can find them in the Project Settings under Input.

Unreal Engine 4 C++ Tutorial

Note: Axis mappings do not need to have the same name as the function you are binding them to.

Add the following inside SetupPlayerInputComponent():

InputComponent->BindAxis("MoveUp", this, &ABasePlayer::MoveUp);
InputComponent->BindAxis("MoveRight", this, &ABasePlayer::MoveRight);

This will bind the MoveUp and MoveRight axis mappings to MoveUp() and MoveRight().

That’s it for the movement functions. Next, you need to enable physics on the Mesh component.

Enabling Physics

Add the following lines inside ABasePlayer():

Mesh->SetSimulatePhysics(true);
MovementForce = 100000;

The first line will allow physics forces to affect Mesh. The second line will set MovementForce to 100,000. This means 100,000 units of force will be added to the ball when moving. By default, physics objects weigh about 110 kilograms so you need a lot of force to move them!

If you’ve created a subclass, some properties won’t change even if you’ve changed it within the base class. In this case, BP_Player won’t have Simulate Physics enabled. However, any subclasses you create now will have it enabled by default.

Compile and then go back to Unreal Engine. Open BP_Player and select the Mesh component. Afterwards, enable Simulate Physics.

Unreal Engine 4 C++ Tutorial

Click Compile and then press Play. Use W, A, S and D to move around.

Unreal Engine 4 C++ Tutorial

Next, you will declare a C++ function that you can implement using Blueprints. This allows designers to create functionality without having to use C++. To learn this, you will create a jump function.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.