In the previous chapter, we learned how to create empty projects and import files, which folder structure to use, and how to work with animations. In this chapter, we’ll explore some other key tools and functionality that you will work with when using Unreal Engine.
Game developers often need to use certain tools that save them time and energy when building game functionality. Unreal Engine’s powerful object inheritance capabilities give developers the edge they need to be more efficient. Developers can also work with both C++ and Blueprints interchangeably and use them to their benefit when developing games.
Another value-added benefit developers gain is the ability to extend code for use later in a project. Let’s say your client has new requirements that build upon the old ones (as is the case in most game studios). Now, to extend functionality, developers can just inherit a class and add more functionality to it to get results quickly. This is very powerful, and it comes in handy in many situations.
This chapter will focus on the Character class in C++. You will be shown how to extend the Character class in C++ and then extend this newly created Character class further in Blueprints via inheritance. You will also work with player input and some movement logic. We will discuss the Unreal Character class, create C++ code, and then extend it in Blueprints, before finally using it to create an in-game character.
In this chapter, we will cover the following topics:
By the end of this chapter, you will understand how class inheritance works in UE5 and how to utilize it to your advantage. You will also be able to work with Axis Mappings and Action Input Mappings, which are key in driving player-related input logic.
This chapter has the following technical requirements:
The complete code for this chapter can be downloaded from GitHub at https://github.com/PacktPublishing/Elevating-Game-Experiences-with-Unreal-Engine-5-Second-Edition.
Before we talk about the Unreal Character class, let’s briefly touch on the concept of inheritance. If you’re used to working with C++ or another similar language, you should already be familiar with this concept. Inheritance is the process whereby a class derives characteristics and behavior from another class. A C++ class can be extended to create a new class – a derived class – that retains properties of the base class and allows these properties to be modified or new characteristics to be added. An example of this is the Character class.
The Character class is a special type of pawn and is a descendant of the Unreal Pawn class. Extending upon the Pawn class, the Character class has some movement capabilities by default, along with some inputs that add movement to the character. As standard, the Character class gives users the ability to get a character to walk, run, jump, fly, and swim within the created world.
Since the Character class is an extension of the Pawn class, it contains all the code/logic of the pawn, and developers can extend this class to add more functionality to it. When extending the Character class, its existing components get carried over to the extended class as inherited components (in this case, the Capsule, Arrow, and Mesh components).
Note
Inherited components cannot be removed. Their settings may be changed, but a component that’s added to a base class will always be present in the extended class. In this case, the base class is the Pawn class, while the extended (or child) class is the Character class.
The Character class provides the following inherited components:
Most developers usually prefer to code the game and character logic in C++ and extend that class to Blueprints so that they can perform other simple tasks, such as connecting assets to the class. So, for example, a developer may create a C++ class that inherits from the Character class, write all the movement and jumping logic within that class, and then extend this class with a Blueprint, in which the developer updates the components with the required assets (such as the Skeletal Mesh and animation blueprint), and optionally code additional functionality into blueprints.
The Character class is extended when it is inherited by either C++ or Blueprints. This extended Character class will be a child of the Character class (which will be called its parent). Extending classes is a powerful part of object-oriented programming, and classes can be extended to great depths and hierarchies.
In this exercise, you will create a C++ class based on a Character class. You will also initialize the variables that will be set in the default values for the class that will extend this Character class.
Follow these steps to complete this exercise:
Note
On some systems, you might be required to run the Unreal Engine editor with administrator privileges to automatically open the Visual Studio solution with the newly created C++ files.
// Spring arm component which will act as a
// placeholder for
// the player camera. This component is recommended to // be used as it automatically controls how the
//camera handles situations
// where it becomes obstructed by geometry inside the
// level, etc
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category =
MyTPS_Cam, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
// Follow camera
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category =
MyTPS_Cam, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
In the preceding code, we’re declaring two components: the Camera component itself and Camera boom, which acts as the placeholder for the camera at a certain distance from the player. These components will be initialized in the constructor in step 11.
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
The preceding code adds the relevant classes to the class, which means we now have access to its methods and definitions.
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// Don't rotate when the controller rotates. Let that // just
affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement
GetCharacterMovement()->bOrientRotationToMovement = true;
// Create a camera boom (pulls in towards the
player if there is a collision)
CameraBoom =
CreateDefaultSubobject<USpringArmComponent>(
TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 300.0f;
CameraBoom->bUsePawnControlRotation = true;
// Create a camera that will follow the character
FollowCamera =
CreateDefaultSubobject<UcameraComponent>(
TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom,
USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
The last line of the preceding code snippet will set up the camera to bind its rotation with the pawns’. This means that the camera should, in turn, rotate based on the rotation of the player controller that’s associated with this pawn.
Figure 3.1 – The Compile button on the top bar of Unreal Editor
A Live coding succeeded message should appear at the bottom right.
Note
You can find the completed exercise code files on GitHub, in the Chapter03 | Exercise3.01 directory, at https://github.com/PacktPublishing/Game-Development-Projects-with-Unreal-Engine/tree/master/Chapter03/Exercise3.01.
After extracting the .rar file, double-click the .uproject file. You will see a prompt asking Would you like to rebuild now?. Click Yes so that it can build the necessary intermediate files, after which it should open the project in Unreal Editor automatically.
By completing this exercise, you’ve learned how to extend the Character class. You have also learned how to initialize the default components of the Character class and how to compile the updated code from within Unreal Editor. Next, you will learn how to extend the C++ class you created in Blueprints and why that is feasible in many situations.
As mentioned earlier, most developers extend the C++ code logic to blueprints to link this with the assets they will use. This is done to achieve easy asset assignment compared to finding and setting up the asset in code. Furthermore, it allows developers to use powerful blueprint features such as Timelines, Events, and ready-to-use macros, in combination with their C++ code, to achieve the maximum benefit of developing with both C++ and Blueprints.
So far, we have made a C++ Character class. In it, we have set up components and movement capabilities. Now, we want to specify the assets that are going to be used in our class, as well as add input and movement ability. For this, it is easier to extend with Blueprint and set up the options there. This is what we will be doing in the next exercise.
In this exercise, you will learn how to extend the C++ class you created with Blueprints to add Blueprint code on top of the pre-existing C++ code. You will also be adding input key bindings, which will be responsible for moving the character.
Follow these steps to complete this exercise:
Note
The ExerciseFiles directory can be found on GitHub at https://github.com/PacktPublishing/Game-Development-Projects-with-Unreal-Engine/tree/master/Chapter03/Exercise3.02/ExerciseFiles.
Figure 3.2 – MixamoAnimPack placed in the project directory
Note
The MixamoAnimPack assets were obtained from the Epic marketplace via the following link: https://www.unrealengine.com/marketplace/en-US/product/mixamo-animation-pack.
Figure 3.3 – Creating the GameMode class
Figure 3.4 – Specifying Game Mode in World Settings
Figure 3.5 – Specifying Default Pawn Class in Game Mode
Note
Meshes and Animations will be covered in depth in Chapter 11, Working with Blend Space 1D, Key Bindings, and State Machines.
Figure 3.6 – Setting up Anim Class and Skeletal Mesh
Figure 3.7 – The Input section of Project Settings
Note
Action Mappings are single keypress actions that are performed, such as jump, dash, or run, while Axis Mappings are float values that are assigned that will return a floating-point value based on the keypress of the user. This is more relevant in the case of gamepad controllers or VR controllers, where the analog thumb stick comes into play. In that case, it would return the floating value of the state of the thumb stick, which is very important for managing player movement or related functionalities.
Figure 3.8 – Movement Axis Mappings
Figure 3.9 – The Event Graph tab
Figure 3.10 – The MoveForward Axis Event
Note
Since the camera associated with a player can choose not to show the pawn’s yaw, roll, or pitch, Get Control Rotation gives the pawn full aim rotation. This is useful in many calculations.
Note
Make Rotator creates a rotator with the pitch, roll, and yaw values, while the break rotator splits a rotator into its components (roll, pitch, and yaw).
Figure 3.11 – Movement logic
Figure 3.12 – The Translation Rotation and Scale Selector section
Once the character is aligned in the capsule, it will look as follows:
Figure 3.13 – Mesh adjusted within the capsule component
Note
You can find the completed exercise code files on GitHub, in the Chapter03 | Exercise3.02 directory, at https://packt.live/3keGxIU.
After extracting the .rar file, double-click the .uproject file. You will see a prompt asking Would you like to rebuild now?. Click Yes on that prompt so that it can build the necessary intermediate files, after which it should open the project in Unreal Editor automatically.
By completing this exercise, you know how to extend C++ code with Blueprints, and why that is favorable in many situations for developers. You also learned how to add input mappings and how they are used to drive player-related input logic.
In the activity for this chapter, you will be combining the skills you have gained from the previous exercises of this chapter and extending the project you completed in Activity 2.01 – linking animations to a character of Chapter 2, Working with Unreal Engine. This will allow you to build on your own Blueprint and see how that maps to real-world scenarios.
Now that you’ve created a C++ class and extended it with Blueprints, it is time to bring both concepts together in a real-world scenario. In this activity, you will be making our character from Activity 2.01 – linking animations to a character, which can be found in Chapter 2, Working with Unreal Engine, jump using the spacebar key on your keyboard. However, you need to create the Character class from scratch in C++ and then later extend it with Blueprint to reach the final goal.
Follow these steps to complete this activity:
Expected Output
The character should be able to jump when you press the spacebar key. The level should use the Blueprint that extends the C++ Character class:
Figure 3.14 – Ganfault jump activity expected output
Note
The solution to this activity can be found on GitHub here: https://github.com/PacktPublishing/Elevating-Game-Experiences-with-Unreal-Engine-5-Second-Edition/tree/main/Activity%20solutions.
By completing this activity, you’ve understood scenarios where C++ code is extended in Blueprints to implement functionalities and logic. This combination of C++ and Blueprints is the most powerful tool game developers possess to create masterful and unique games within Unreal Engine.
In this chapter, you learned how to create a C++ Character class, add initializer code to it, and then use Blueprints to extend it to set up assets and add additional code.
The result obeys the C++ code, as well as the Blueprint code, and can be used in any purposeful scenario.
You also learned how to set up Axis Mappings that have been mapped to the W, A, S, and D keys to move players (which is the default movement mapping in many games). You also learned how to make the character jump within the game.
In the next chapter, you will explore Input Mappings in more depth and how to use the Mobile Previewer within Unreal Editor. This will help you create games with solid inputs mapped to game and player logic. It will also allow you to quickly test what your game will look and feel like on a mobile, all within Unreal Editor.
3.144.45.137