Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elevating Game Experiences with Unreal Engine 5

You're reading from   Elevating Game Experiences with Unreal Engine 5 Bring your game ideas to life using the new Unreal Engine 5 and C++

Arrow left icon
Product type Paperback
Published in Sep 2022
Publisher Packt
ISBN-13 9781803239866
Length 760 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Authors (4):
Arrow left icon
Gonçalo Marques Gonçalo Marques
Author Profile Icon Gonçalo Marques
Gonçalo Marques
Devin Sherry Devin Sherry
Author Profile Icon Devin Sherry
Devin Sherry
David Pereira David Pereira
Author Profile Icon David Pereira
David Pereira
Hammad Fozi Hammad Fozi
Author Profile Icon Hammad Fozi
Hammad Fozi
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Chapter 1: Introduction to Unreal Engine 2. Chapter 2: Working with Unreal Engine FREE CHAPTER 3. Chapter 3: Character Class Components and Blueprint Setup 4. Chapter 4: Getting Started with Player Input 5. Chapter 5: Query with Line Traces 6. Chapter 6: Setting Up Collision Objects 7. Chapter 7: Working with UE5 Utilities 8. Chapter 8: Creating User Interfaces with UMG 9. Chapter 9: Adding Audio-Visual Elements 10. Chapter 10: Creating the SuperSideScroller Game 11. Chapter 11: Working with Blend Space 1D, Key Bindings, and State Machines 12. Chapter 12: Animation Blending and Montages 13. Chapter 13: Creating and Adding the Enemy Artificial Intelligence 14. Chapter 14: Spawning the Player Projectile 15. Chapter 15: Exploring Collectibles, Power-Ups, and Pickups 16. Chapter 16: Getting Started with Multiplayer Basics 17. Chapter 17: Using Remote Procedure Calls 18. Chapter 18: Using Gameplay Framework Classes in Multiplayer 19. Index 20. Other Books You May Enjoy

Understanding levels and the Level Blueprint

Levels, in gaming, are sections or parts 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. When they are done with that level, 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 the 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 within 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 DefaultPawn class

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 class

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 allows you 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. (This is 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 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 classes

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 classes to the game and testing if our code works in Blueprints.

Follow these steps to complete this exercise:

  1. Open the project we created in Exercise 2.01 – creating an empty C++ project.
  2. Right-click inside the Content Browser area and select Blueprint Class.
  3. Under the ALL CLASSES section, find and select Game Mode:
Figure 2.16 – Selecting the Game Mode class

Figure 2.16 – Selecting the Game Mode class

  1. Set its name to BP_MyGameMode.
  2. Repeat steps 2 to 4 and select the Pawn class from under the Common Classes section, as shown in the preceding screenshot. Set the name of this class to BP_MyPawn.
  3. Repeat steps 2 to 4 and select the Player Controller class under the Common Classes section, as shown in the preceding screenshot. Set the name of this class to BP_MyPC:
Figure 2.17 – Game Mode, Pawn, and Player Controller names

Figure 2.17 – Game Mode, Pawn, and Player Controller names

  1. Open BP_MyGameMode and open the Event Graph tab:
Figure 2.18 – The Event Graph tab in Blueprint

Figure 2.18 – The Event Graph tab in Blueprint

  1. Left-click and drag from the white pin in the Event BeginPlay node and then release the left mouse button to open the Options menu. Type print and select the print node highlighted in the list:
Figure 2.19 – The Print String node (Blueprint)

Figure 2.19 – The Print String node (Blueprint)

  1. In the resultant Print String node that gets placed under the In String parameter, type My Game Mode has started!.
  2. Now, press the Compile and Save buttons on the top menu bar.
  3. Repeat steps 7 to 10 for both the BP_MyPawn and BP_MyPC classes, setting the In String parameter to My Pawn has started! and My PC has started!, respectively.
  4. Finally, open the World Settings tab by clicking Settings on the right of the editor and clicking on World Settings:

Figure 2.20 – World Settings

Figure 2.20 – World Settings

  1. Under the Game Mode section, use the dropdown to set the GameMode Override, Default Pawn Class, and Player Controller Class options to our respective classes:
Figure 2.21 – World Settings and Game Mode setup

Figure 2.21 – World Settings and Game Mode setup

  1. Click Play to play your game and see the three print statements on the top. This means that the current GameMode Override, Default Pawn Class, and Player Controller Class options have been set to your specified classes and are running their code:
Figure 2.22 – Output prints

Figure 2.22 – Output prints

Note

You can find the completed exercise code files on GitHub, in the Chapter02 | Exercise2.04 | Ex2.04-Completed.rar directory, at 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 look at animations, what processes are involved, and how they complete them. We’ll follow this with an exercise.

You have been reading a chapter from
Elevating Game Experiences with Unreal Engine 5 - Second Edition
Published in: Sep 2022
Publisher: Packt
ISBN-13: 9781803239866
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image