Levels
Levels, in gaming, are a section or a part of a game. Since many games are quite large, they are broken down into different levels. A level of interest is loaded into the game for the player to play, and then when they are done with that, another level may be loaded in (while the current one will be loaded out) so that the player can proceed. To complete a game, a player usually needs to complete a set of specific tasks to move on to the next level, eventually completing the game.
A Game Mode can be directly applied to the level. The level, upon loading, will use the assigned Game Mode class to handle all logic and gameplay for that particular level and override the game mode of the project for this level. This can be applied using the World Settings
tab after opening a level.
A Level Blueprint is a blueprint that runs with the level, but cannot be accessed outside the scope of the level. Game Mode can be accessed in any blueprint (including the level blueprint) by the Get Game Mode
node. This can later be cast to your Game Mode class, to obtain a reference to it.
Note
A level can only have one Game Mode class assigned to it. However, a single Game Mode class can be assigned to multiple levels to imitate similar functionality and logic.
The Unreal Pawn Class
The Pawn
class, in Unreal, is the most basic class of actors that can be possessed (either by a player or AI). It also graphically represents the player/bot in the game. Code inside this class should have everything to do with the game entities, including interaction, movement, and ability logic. The player can still only possess a single pawn at any time in the game. Also, the player can unpossess one pawn and possess another pawn during gameplay.
The Default Pawn
Unreal Engine gives developers a DefaultPawn
class (which inherits from the base Pawn
class). On top of the Pawn
class, this class contains additional code that allows it to move within the world, as you would in the editor version of the game.
The Spectator Pawn
Some games offer features to spectate games. Let's say you're waiting for a friend to finish their game before joining you, so you go ahead and spectate their game. This gives you the ability to observe the game the player is playing, through a camera that you can move around to get a view of the players or the game. Some games also offer spectate modes that can travel back in time, to show a particular action of the game that happened in the past or at any point in the game.
As the name suggests, this is a special type of pawn that provides sample functionality to spectate a game. It contains all the basic tools (such as the Spectator Pawn Movement component) required to do so.
The Unreal Player Controller Class
The Player Controller class can be thought of as the player. It is essentially the soul of a pawn. A Player Controller takes input from the user and feeds it to the pawn and other classes for the player to interact with the game. However, you must take note of the following points while dealing with this class:
- Unlike the pawn, there can only be one Player Controller that the player represents in a level. (Just like when you travel in an elevator. While inside one, you can only control that elevator, but you can then exit it and enter another elevator in order to control that one.)
- The Player Controller persists throughout the game, but the pawn may not (for example, in a battle game, the player character may die and respawn, but the Player Controller would remain the same).
- Due to the temporary nature of the pawn and the permanent nature of the Player Controller, developers need to keep in mind which code should be added to which class.
Let's understand this better through the next exercise.
Exercise 2.04: Setting Up the Game Mode, Player Controller, and Pawn
This exercise will use the blank project we created in Exercise 2.01, Creating an Empty C++ Project. We'll be adding our Game Mode, Player Controller, and Pawn
class to the game and will be testing to see if our code works in Blueprints.
The following steps will help you complete this exercise:
- Open the project we created in Exercise 2.01, Creating an Empty C++ Project.
- Right-click inside the
Content Browser
and selectBlueprint Class
. - Under the
All Classes
section, find and select theGame Mode
class: - Set its name to
BP_MyGameMode
. - Repeat Steps 2-4 and select the
Pawn
class from under theCommon Classes
section, as shown in the preceding screenshot. Set the name of this class toBP_MyPawn
. - Repeat Steps 2-4 and select the
Player Controller
class under theCommon Classes
section, as shown in the preceding screenshot. Set the name of this class toBP_MyPC
: - Open
BP_MyGameMode
and open theEvent Graph
tab: - Left-click and drag from the white pin in the
Event BeginPlay
node and then release the left mouse button to gain anOptions
menu. Typeprint
and select theprint
node highlighted in the list: - In the resultant
Print String
node that gets placed under theIn String
parameter, typeMy Game Mode has started!
. - Now, press the
Compile
andSave
buttons on the top menu bar. - Repeat Steps 7-10 for both the
BP_MyPawn
andBP_MyPC
classes, setting theIn String
parameter toMy Pawn has started!
andMy PC has started!
, respectively. - Finally, open the
World Settings
tab, and under theGame Mode
section, use the dropdown to set theGameMode Override
,Default Pawn Class
, andPlayer Controller Class
options to our respective classes: - Click
Play
to play your game and see the three print statements on the top. This means that the currentGameMode Override
,Default Pawn Class
, andPlayer Controller Class
options have been set to your specified classes and are running their code:
Note
You can locate the completed exercise code files on GitHub, in the Chapter02
-> Exercise2.04
-> Ex2.04-Completed.rar
directory, at the following link: https://packt.live/3k7nS1K
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.
Now that you know the basic classes and how they work in Unreal, in the next section, we will be looking at animations, what processes are involved, and how they complete them. We'll follow this with an exercise.