Chapter 5. Upgrade Activated – Making Bounty Dash with C++

Welcome to Chapter 5! Well done on working through the introduction to C++ chapter! We are now going to utilize and build on those skills to produce your first C++ based Unreal Engine title, Bounty Dash! During this chapter, you will leverage your own technical knowledge and that of this book to create a 3D endless runner! This chapter has been designed so that we utilize as much C++ as possible, to create this project so that we may maximize your C++ learning. Once you have completed this chapter and the following, you will be well on your way to becoming a capable Unreal Engine 4 C++ programmer!

During this chapter, you are going to learn some core techniques that will provide you with the knowledge to write effective and efficient gameplay code. You will learn how to create a game character utilizing C++ and to access in level objects from C++. You will learn how to get components and metadata from objects using C++. You will use all of this knowledge to make a game with C++ and Unreal Engine! It should be noted that we are specifically taking a C++ oriented approach when developing this project, meaning that we may sacrifice development efficiency for the sake of learning. The next project started in Chapter 7, Boss Mode Activated – Unreal Robots will be developed with hybrid, efficient techniques in mind. In a list, we are going to be learning the following topics:

  • Creating and extending the C++ basic template
  • Creating character objects using C++
  • Utilizing blueprint to extend C++ objects for easier asset association
  • Triggering audio and animation cues in C++
  • Referencing in level objects within C++ objects
  • Referencing Blueprint objects in C++ with the UCLASS type
  • Creating a game mode with C++
  • Obtaining object metadata with C++
  • Communicating with the custom C++ game mode

Creating a C++ character

Alright! Let's start the development of Bounty Dash by creating the character that we will be using for this project. Due to the nature of an endless runner, we can create a fairly simple input and movement implementation for the character. This is a great place to start with creating C++ characters, as they can quickly become complicated and involved!

Create the C++ project

Before we start building the character, we first need to create the project. Open Unreal Engine 4.10 and select the Basic Code project template we used for the last chapter. This time call the project Bounty Dash. Upon opening the project, select New Level… from the File dropdown. From the available map options, choose Default. This will provide us with a nice simple base to work from. Save this map as BountyDashMap.

The gameplay for BountyDash will be quite simple, there will be a seemingly infinite progression of objects that the player is running towards. The player must dodge the objects by swapping between three lanes. Coins will also appear periodically; the player must collect these coins to boost their score. Once a certain number of coins have been collected, the game will speed up. If the player hits an obstacle, the player will be stalled. If the player is stalled for too long in total, he will be killed by a constantly encroaching wall of death. We will definitely be expanding on these game rules as we work through the project; but for now, this a good base to work from.

The UE4 object hierarchy

As we mentioned in the previous chapters, there is a base object hierarchy that all unreal objects will inherit from. It is important to know the various stages of this hierarchy and the feature set each subsequent base class in a hierarchy brings to the table. The first thing that you must know is that all Unreal objects will inherit from the UObject base class. This means that every object that interacts with the engine in some way can be processed and stored generically via the UObject type, affording the use of the engine's generic object management systems such as the Unreal Garbage Collector.

When it comes to objects that exist in a level, we have already covered a brief description of objects and actors. Before we go any further with our C++ journey, a more descriptive breakdown is required. The object hierarchy for a UE4 ACharacter object is as follows:

The UE4 object hierarchy

Each of the classes listed in this hierarchy add to the feature set of the object defining it as an ACharacter. Each of the classes featured in this hierarchy can be separated with regards to the role each class assumes. They can be broken down as follows:

  • UObjectBase: This is the UObject base class from which all objects inherit. It includes generic functions that all objects will require. An example of one said function is GetClass(), which will return the class type of this object. This means that we are able to type check against all UObject classes.
  • UObjectBaseUtility: This expands the function set that only depends on UObjectBase. This includes metadata functions similar to that of GetClass() that allows developers to get various information from their objects.
  • UObject: This is the base class of all objects. As mentioned earlier, any unreal object will inherit from this class. Again, this class includes methods that the epic team wished all unreal objects to have.
  • AActor: The base class for any object that can be placed or spawned in a level. AActors can (and will) include Actor Components that control how they move, render, and process. It is from this base class that we are able to make tickable actors that can be seen in levels. It is also important to note that the other main function of the AAction base class is the inclusion of the various networking features provided by the engine. This will be covered in Chapter 9, Creating a Networked Shooter.
  • APawn: This is the base class for all actors that can be possessed and controlled by either player controllers or AI. APawns are the physical representations of players and creatures in levels. This provides the developers with a base class for all controllable objects, which is why a lot of the input functions we have been working with so far (in Blueprint and C++) will return or take an APawn reference.
  • ACharacter: These are similar to pawns, but will include Meshes, collision, and in-built movement logic by default. They are responsible for all interactions that take place between a player or AI and the game world. ACharacters expand on the networking features mentioned in the AActor base class. We have already worked with a Blueprint abstraction of an ACharacter in BarrelHopper. As we know, ACharacters include a CharacterMovementComponent that will dictate how the character moves through the game world based on various movement modes.

Tip

If you wish to find out more about the ACharacter base class and hierarch address the following API page—https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/ACharacter/index.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.190.38