Creating a Custom Pawn and Player Controller
In UE4, Actors that are controlled directly by players or artificial intelligence (AI) are called Pawns. These Pawns can be practically anything: dinosaurs, humans, monsters, vehicles, bouncy balls, spaceships, even animate food. Any player- or AI-controlled entity in a game is a Pawn. Some games may not have physical or visible representations of players, but Pawns are still used to represent the physical locations of players in the game world.
Pawns define the visible appearance of the controlled objects and also can control movement, physics, and abilities. It is often useful to think of them as the physical bodies of the player in the game world.
The non-physical representation of a player is a Controller. Controllers are the interface between a Pawn and the player or AI controlling it.
Controllers are Actors that can possess and control Pawns. Again, Controllers are non-physical and usually do not directly determine physical properties (e.g., appearance, movement, physics) of the possessed Pawn. Instead, they are more the representation of the will or intent of the player.
There is a one-to-one relationship between Controllers and Pawns—in other words, one Controller per Pawn and one Pawn per Controller. With this in mind, Pawns can be possessed (i.e., controlled) by AI through an AI Controller or by a player through a Player Controller.
The default Player Controller handles most behavior you need for your game, but you should create your own Pawn.
Inheriting from the Default Pawn
To create a Pawn, you can create a new Blueprint class. This time, however, you start with a class from the All Classes section of the Pick Parent Class window that has a few more features already premade for you. When you create a Blueprint class, you expand the All Classes to get access to all the classes in the project. As shown in Figure 20.3, you can look through this list for specific classes. In this case, you want to use the DefaultPawn class because it automatically sets up some of the behaviors that you are going to need in your game.
FIGURE 20.3 In the Pick Parent Class window, expand the All Classes subsection and search for the Pawn you want, such as DefaultPawn.
You now have a new Pawn class, and you need to understand the different parts that make up the class. Double-click your new Hero_Spaceship class in the Content Browser to open it in the Blueprint Class Editor.
Look at the component hierarchy. By default, there are three components in a DefaultPawn class: CollisionComponent, MeshComponent, and MovementComponent (see Figure 20.4). These three components handle the major types of behaviors a Pawn is responsible for.
FIGURE 20.4 The component hierarchy in the Blueprint Class Editor for a DefaultPawn class.
CollisionComponent handles both physics collisions of the Pawn and trigger overlaps of the Pawn with volumes or Actors in the level. It represents the physical volume of the Pawn and can be shaped to fit the Pawn’s simplified form. CollisionComponent does not show up in a game and is not part of the Pawn’s visual representation.
MeshComponent controls the visuals in a game. Right now for your game, this MeshComponent class is a sphere, meaning that the visual representation of your Pawn is a sphere. You can replace or modify MeshComponent to make your Pawn look like anything you desire. You can add other types of components here to change the visuals, including Particle Emitters, Skeletal Meshes, 2d Sprites, and complex hierarchies of Static Meshes.
MovementComponent controls your Pawn’s movement. Using MovementComponent is a convenient way of handling player movement. Complex tasks (such as checking for collision and handling velocity) are simplified through the convenient interface of MovementComponent.
Because you haven’t changed it yet, your Pawn is currently just a simple sphere. You can change this by replacing MeshComponent completely or by changing its Static Mesh reference. In the next Try It Yourself, you will import the UFO mesh used by many of the UE4 content examples and then replace the current Pawn’s mesh with it.