© Satheesh Pv 2021
S. PvBeginning Unreal Engine 4 Blueprints Visual Scriptinghttps://doi.org/10.1007/978-1-4842-6396-9_4

4. Gameplay Classes

Satheesh Pv1  
(1)
Mumbai, India
 

This chapter looks at some of the important and common gameplay classes you use in almost all projects.

Actor

The Actor class is the base for all placeable and spawnable objects in a game world. Anything that can be dragged and dropped into the world or spawned at runtime is inherited from the Actor class. Actor classes are basic classes that can support translation (position), rotation, and scale (size). Actors can contain any number of Actor Component classes that define how it should move and render. By default, Actors are generic classes and do not have any kind of visual representation. It is up to the component to give it a visual representation. Actor classes also support replication and function calls across the network.

Game Mode Base

Game Mode Base defines the basic rules for your game, such as which actors are allowed, player scores, winning rules, and so forth. It holds information about all the players in the game world. For example, you can define the score required to win a match. Game Mode sets the classes for Pawn, Player Controller, Game State, Player State, Spectator, and more. In a multiplayer game, the Game Mode class only exists on the server, and for the client, it is always null, as if it never exists. This is only applicable to multiplayer games because, in single-player games, there are no clients, only the server.

Game Mode, a subclass of Game Mode Base, is designed toward multiplayer games. It contains default behavior for picking spawn points for each player.

Game State Base

Game State Base defines the current state of the game. Game State classes are spawned by the Game Mode. They are particularly useful in a multiplayer game because Game State classes are replicated to every client, so any change that happens to a replicated value reflects all clients. For example, imagine you have a game mode that requires some number of kills to win the game. Every time the player eliminates another player, it runs a server function that updates the current number of total kills in Game Mode, which is then changed in the game state, and it is updated on all clients. Game State is a subclass of Game State Base.

Game Instance

The Game Instance class is created when you run the game. It exists when the game is running. It is useful for transferring information between levels. For example, imagine in your game that the player finishes a certain challenge. You can store this specific information in Game Instance. When the player returns to the main menu, you can show a message on the screen that says the challenge has been completed. Game Instance is a UObject-based class; it is not replicated to anyone. The server or client is not aware of any other game instances other than their own.

Game Instance is set via Project Settings ➤ Maps & Modes ➤ Game Instance class (see Figure 4-1).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig1_HTML.jpg
Figure 4-1

Setting Game Instance class

The following describes the three main Game classes.
  • Game Mode holds authority of the game flow and only exists on the server, which prevents cheating.

  • Game State holds the replicated state of the game. It exists on the server and client. Clients can ask the Game State about the current state of the game, and the server can modify the current state of the game, which is replicated to all clients.

  • Game Instance is a true persistent class that is guaranteed to be available and accessible from the very beginning of your game until you exit. It is not replicated and exists on both the server and client.

Pawn

Pawn is an Actor-based class that acts as a physical representation of a player or artificial intelligence (AI). Pawns are controlled by a Controller, which is either a Player Controller or an AI Controller. Even though the pawn does not have visual representation by default, it still represents location, rotation, and so forth, within the game world. Pawns can contain their own movement logic, but it’s better done in the Controller. Pawns can be replicated across the network.

Character

Character is extended from the Pawn class to support visual representation by using the Skeletal Mesh Component for animations. It also includes the Capsule Component to simulate movement collisions, and the Character Movement Component, a feature-rich component that includes movements such as walk, run, jump, swim, and fly. It also includes basic networking support, which makes it a first-class choice for bipedal characters. The Character Movement Component is specific to the Character class, so it cannot be used in any other class.

Player Controller

Player Controllers are subclassed from the Controller, which is essentially the brain of the Pawn it is controlling. Based on the context, the Controller is a Player Controller if it is controlled by a human player (and can handle all player inputs) or an AI Controller is controlled by artificial intelligence.

By default, a Controller can only control one Pawn at any given time by calling the Possess() function. It stops controlling by calling the UnPossess() function. A Controller also allows notifications from the Pawn class that it is controlling. In a networked game, the server is aware of all controllers while the client is only aware of its own local controller.

Player State

Player State is a replicated Actor that is available for every player. There is only one player state in a single-player game, but in a multiplayer game, each player has their own player state. Since it is replicated, the server and client are aware of all player states, and all of them can be accessed from the Game State class. This is a good place to store network-relevant information about a player, like score, player name, and so forth.

Let’s share a scenario where you can use these classes. When writing this book, battle royale games (Fortnite, PUBG, etc.) are very popular among us gamers, so if you were to create a battle royale game, you could use the following classes.
  • Game Mode: This class defines the rules. In this class, you track the number of players that have entered the match, the number that are alive, the number that are dead, and the quitters. This class also tracks and decides other game elements, such as a battle plane (or a battle bus in Fortnite), circle, and so on, and whether the match has started or is waiting to start (warm up).

  • Game State: This class tracks the replicated state of the game. For example, when the game mode transitions from prematch (lobby) to a match state (battle plane or bus starting on top of the map), it notifies the game state that the state has changed, and the game state can notify all the clients that match started.

  • Game Instance: This class saves information about any objectives or challenges completed in the game so that the player can be rewarded when they return to the main menu.

  • Character: The visual representation of the character.

  • Pawn: A vehicle or an animal in your game. (Optional)

  • Player Controller: This class handles all the input and creates and manages the user interfaces. It can also send RPCs (remote procedure calls) to the server. The server is aware of all player controllers (e.g., if the game has 100 players, the server knows all 100 player controllers), but the client is only aware of its own.

  • Player State: This class holds the information related to player-specific states (e.g., the number of players you killed, the amount of ammo you fired, and so forth).

Creating Character Class and Player Controller Class

In this section, you learn how to create a new character class that allows the player to move around in the game world using the Player Controller class. Later in this section, a UMG (Unreal Motion Graphics) widget shows health information to the player.

To create a new character class, right-click the Content Browser and select Blueprint Class. You are prompted to pick a parent class for your Blueprint. Select Character from the Common Classes section (see Figure 4-2).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig2_HTML.jpg
Figure 4-2

Selecting character class

Next, open the Character Blueprint. You see the default Capsule Component, Mesh Component, and Character Movement Component (see Figure 4-3).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig3_HTML.jpg
Figure 4-3.

Default components

  • CapsuleComponent is the actor’s root component. It is responsible for collisions. CharacterMovement is aware of this capsule component and moves it around according to the player input.

  • ArrowComponent is an editor-only component that helps indicate which direction the object is facing.

  • Mesh is the visual representation of the character and is of the Skeletal Mesh Component type.

  • CharacterMovement handles the movement logic of the associated character. This component updates the location and rotation of CapsuleComponent, which makes the character move. It is a highly featured component and includes network support.

We will use the default Mannequin that comes with the Engine as our character. To use it, you must add the Third Person Project to your project. If not yet added, you can do it by clicking the Add New button in the Content Browser and selecting Add Feature or Content Pack (see Figure 4-4).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig4_HTML.jpg
Figure 4-4.

Adding feature pack or starter content from Content Browser

Next, select Third Person from the Blueprint Feature tab and click Add To Project. Now you have the Mannequin. Assign the Mannequin to the previously created Character Blueprint class.

You may notice that the mesh is not aligned properly. The character mesh is at the center of the Capsule Component, and the rotation of the character is facing in the wrong direction. To fix this, adjust the location and rotation of the Mesh Component to the proper values.
  • Location: X: 0, Y: 0, Z: –97

  • Rotation: Roll: 0, Pitch: 0, Yaw: 270

You also need to add a camera to the player to have a proper view. First, you add a Spring Arm Component, which maintains its children at a fixed distance from its parent. To do this, click the Add Component button in the Components tab and select Spring Arm under the Camera section (see Figure 4-5).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig5_HTML.jpg
Figure 4-5.

Add spring arm component

Using the same method, add a Camera to the Spring Arm Component. The result should look like Figure 4-6.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig6_HTML.jpg
Figure 4-6.

After adding Camera to Spring arm

If you drag and drop the character into the Level Editor, you can preview how the player sees the character from this camera angle. There are few settings to change in the Character class. Click the Class Defaults button in the Blueprint Editor toolbar. In the Details panel, disable Use Controller Rotation Yaw under the Pawn section. After that, select the Spring Arm Component that you added and enable Use Pawn Control Rotation under Camera Settings. Finally, select the Character Movement Component and enable Orient Rotation to Movement.

Now that our character is almost ready, let’s implement a basic function that moves it around.

First, you need to add two functions that make the character move forward/backward and left/right. To create a new function, click the Function button under the Functions section in the My Blueprint tab in your Character class. After you click the button, you can rename the function; let’s call it Move Forward or Backward .

You also add a float parameter called ScaleValue, which scales the input. To add a parameter to the function, select the purple node, and in the Details panel, add a new input parameter and select Float from the drop-down list (see Figure 4-7).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig7_HTML.jpg
Figure 4-7.

Adding input to function

Inside this function, you get the character’s control rotation. You use its yaw rotation to find the forward vector, which you use as the direction to move the character. The forward vector is a normalized vector that points in the direction that the actor is facing. The final function should look like what’s shown in Figure 4-8.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig8_HTML.jpg
Figure 4-8.

Final function

The same function can be duplicated; call it Move Left or Right. Rather than the Get Forward Vector, use the Get Right Vector. The final function should look like Figure 4-9.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig9_HTML.jpg
Figure 4-9.

Same function with Get Right Vector

Note that the two functions look the same except for Get Forward Vector and Get Right Vector.

Before continuing with input, you need to define these functions in Input Settings under the Engine section, which is accessed from Project Settings. In Figure 4-10, you see the basic input settings that I defined.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig10_HTML.jpg
Figure 4-10.

Adding inputs via Project settings

The axis values can be accessed from a Pawn class or Controller class. We will use the latter option.

To create a new player controller, right-click Content Browser, and select Blueprint Class. In the next window, select Player Controller and open the asset to add nodes. Inside the Player Controller graph, right-click the graph and search for MoveForward, which is the same name you defined in Input settings. Once you select the node, right-click the graph again and search for Get Controlled Pawn. The return value of the Get Controlled Pawn node points to the current character or pawn the player is controlling. So from the return value, drag a pin and cast it to the Character class that you created. From the Casted Character class, drag another pin and call the Move Forward or Backward function you created. Connect this set of nodes to the InputAxis MoveForward event. The same set of nodes connect to the InputAxis MoveRight event, but instead of using the Move Forward or Backward function, you call the Move Right or Left function. The final graph should look like Figure 4-11.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig11_HTML.jpg
Figure 4-11.

Final graph with all functions

The last step uses the Character and Controller classes in our game. To do so, create a new Game Mode Blueprint and assign our character as the default pawn and the controller as our Player Controller class in Game Mode. In Figure 4-12, you can see I assigned our Blueprint-created character and controller to the Game Mode class.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig12_HTML.jpg
Figure 4-12.

Assign controller and pawn in game mode

To use BP_GameMode we created , you need to assign this to your level’s World Settings. In your Level Editor’s main toolbar, click Settings and select World Settings (see Figure 4-13).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig13_HTML.jpg
Figure 4-13.

Select world settings from settings

In the World Settings tab, assign the game mode (see Figure 4-14).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig14_HTML.jpg
Figure 4-14.

Assign game mode

If you press Play, you can move the character around using the W, A, S, D keys, or any keys you assigned in Input Settings.

Creating and Showing Data on HUD

In this section, you add a basic HUD to the player character, create a health variable that changes when the player takes damage, and create a welcome text that automatically disappears after 3 seconds. Note that the implementation shown here only works in single-player games; it does not work in multiplayer games.

First, let’s create a UMG widget Blueprint by right-clicking the Content Browser and selecting User Interface ➤ Widget Blueprint (see Figure 4-15).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig15_HTML.jpg
Figure 4-15.

Creating widget blueprint

Open the Widget Blueprint. You see the main Designer section, where you drag and drop different widgets from the Palette panel. In the top-right corner, you can switch between Designer view and Graph view (see Figure 4-16). Graph view is used to create all the Blueprint logic.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig16_HTML.jpg
Figure 4-16.

UMG layout

Let’s drag and drop a progress bar widget from the palette to the Designer tab. Select the progress bar widget. In the Details panel, enter a name for the progress bar (e.g., HealthBar)(see Figure 4-17).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig17_HTML.jpg
Figure 4-17.

Setting variable name

To implement the logic, let’s first switch to the Graph view and create a new event. Right-click inside the graph and select Add New Event from the Add Event category. Rename this event to UpdateHealth and add two float parameters called CurrentHealth and MaxHealth. Drag from the CurrentHealth float pin, search for the Divide operator, and select it. Then connect MaxHealth to the B input of Divide. This is the result that you use in ProgressBar. It is done because ProgressBar works in the 0–1 range, and since you are dividing CurrentHealth by MaxHealth, you get the result in this range.

To set the value of ProgressBar, drag and drop ProgressBar from the Variables category to the Graph view, drag from the pin, and search for Set Percent. Connect the result of the Divide node to the In Percent of Set Percent node. Your node setup should look similar to Figure 4-18.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig18_HTML.jpg
Figure 4-18.

Update health setup

To use of widget, you need to add it to the screen. Let’s open the Character Blueprint and switch to Event Graph. Right-click inside the graph and search for the Create Widget node. Inside the Class input, set the newly created UMG HUD. Then right-click Return Value and choose Promote To Variable (see Figure 4-19). Name it PlayerHUD. This caches the result to a variable so that you can access it anytime you want.
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig19_HTML.jpg
Figure 4-19.

Promoting to variable

From the PlayerHUD variable, drag a pin, search for Add To Viewport, and select it (see Figure 4-20).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig20_HTML.jpg
Figure 4-20.

Adding widget to viewport

If you play now, you can see the newly added HUD on your screen, but it does nothing yet because we have not called the Update Health event. To quickly fix this, drag a pin from the PlayerHUD variable and search for UpdateHealth. Now create a float variable called Health, and set its default value to 100. Drag this variable from the My Blueprint tab, connect it to the Current Health of Update Health function, and set Max Health to 100 (see Figure 4-21).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig21_HTML.jpg
Figure 4-21.

Calling update health event initially

Once you have set up everything, add the welcome text. Drag and drop the Text widget from the Palette tab to the designer. To align this text in the center of the screen, set the Anchor to center by expanding it and setting both Minimum and Maximum to 0.5. You need to set the alignment to 0.5 also. After that, set Position X and Position Y to 0. The text is now centered on the screen. Next, enable Size To Content to automatically size the widget according to the text.

Under the Content category, change the text to your liking. For this example, let’s write something like Welcome to UMG. Expand the font under Appearance and set Size to 48. You need the text to disappear after 3 seconds. To do so, make sure that the Is Variable in the Details tab is set to True and enter a name for your Widget (e.g., WelcomeText) (see Figure 4-22).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig22_HTML.jpg
Figure 4-22.

Setting variable name for text

Switch to Graph view, and you now can drag and drop the WelcomeText widget from the MyBlueprint tab (under the Variables section) to the graph. Right-click the graph, search for Delay, and select it. Connect the Delay node to Event Construct. Then drag a wire from the output pin of Widget Text, search for Set Visibility, and select it. Change In Visibility from Visible to Collapsed, and connect the output Completed of Delay node to the Set Visibility node. Finally, change the duration of the Delay node to 3 seconds (see Figure 4-23).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig23_HTML.jpg
Figure 4-23.

Graph with delay node

Click the Play button. You see that the welcome text and progress bar are showing (see Figure 4-24).
../images/496849_1_En_4_Chapter/496849_1_En_4_Fig24_HTML.jpg
Figure 4-24.

Playing game with UMG widget added

Whenever your player is taking damage, you can reduce your health and call Update Health with Health connected to Current Health, which properly updates the progress bar in UMG.

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

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