Chapter 6. NPCs and Dialog

By now, you already have a player character by default that interacts with battles on the field, but the game is in dire need of non-player characters (NPCs).

In this chapter, you will create an NPC that will act as an information hub and shop owner. Since we have not yet given useable items and equipment to the character, or a reason for the character to have gold, the next logical step is to create a shop owner who will play the role of an NPC by talking to the player and offering the player business transactions when the player decides to interact with the NPC.

We will cover the following topics in this chapter:

  • Creating the NPC Character Blueprint
  • Interacting with the NPC
  • Dialog box setup
  • Creating an NPC welcome box
  • Adding an NPC talk box

Creating the NPC Character Blueprint

To get started, we will need to create a new Character Blueprint class. Since we already have a location for characters, navigate to the character folder located in your Content Browser under Content | Blueprints | Characters (create a new character folder in content/blueprints if one is not there already, just to stay more organized; you can even drag your FieldPlayer Blueprint into the character folder if you'd like), and add a new character by clicking on Add New | Blueprint Class:

Creating the NPC Character Blueprint

The Pick Parent Class window will pop up. This will allow you to pick a common parent class for your Blueprint class or create your own class:

Creating the NPC Character Blueprint

UE4 has a lot of common classes built in, which have a lot of the frameworks of several different types of classes already built for us. One of those classes is Character which allows us to work with a common character framework for use with any type of character pawn we want. So, from this window, select Character.

Once you have selected Character, a new Character Blueprint should now be in Content | Blueprints | Characters. Rename the character as NPC_ShopOwner since we will be using this character as the shop owner:

Creating the NPC Character Blueprint

From here, open NPC_ShopOwner to enter the viewport of your new character:

Creating the NPC Character Blueprint

You should see that, as mentioned earlier, this class inherits Character.h which already has a framework for a lot of components. This will now make it easy for us to make a visible NPC. Within the Components panel, you will see CapsuleComponent, which holds inherited data such as ArrowComponent that determines which way the object is pointing, and Mesh, which holds an instance of a skeletal mesh.

Let us first apply a mesh and animations to our Mesh component. Since we are not creating character models and animations in this book, we can simply use built-in assets. First, click on Mesh within Components | CapsuleComponent:

Creating the NPC Character Blueprint

From here, you will notice that your Details panel changes in order to display the Mesh variable and its components. For now, we will want to keep most of the defaults here since the character has everything we would want out of an NPC. However, the character does not have a mesh, so let's give it one by navigating to Details | Mesh and, in Skeletal Mesh, selecting the None drop-down menu that would house a skeletal mesh. Then, you should see all the skeletal meshes available in our project. In our game, there is a skeletal mesh called SK_Mannequin, we will select this as our skeletal mesh:

Creating the NPC Character Blueprint

Now that we have selected a skeletal mesh, you should see it displayed in your viewport. At this point, you may notice that your skeletal mesh is much higher than the bottom of the capsule because the skeletal mesh's origin is at its feet and is attached to the origin of the capsule that is located in the middle of the capsule. There are a lot of ways to fix this problem, but the quickest way would be to reposition the character manually by bringing the character down on the Z axis. A value of -90 for Z Location seems to work perfectly in this example:

Creating the NPC Character Blueprint

You will also want the character to be facing the right way. You can see that the character is faced the wrong way because the arrow component is pointing perpendicular to the way the character is facing. Simply adjust the character rotation on the Z axis. A value of -90 for Z Rotation seems to do the trick:

Creating the NPC Character Blueprint

Lastly, we need to get the character out of the relaxed pose. So navigate to Details | Animation and select the Anim Class drop-down menu. In this drop-down menu, select ThirdPerson_AnimBP, which is the animation class for the character we selected. If you are using another character with a different animation class, be sure to select the animation class that is built for your character:

Creating the NPC Character Blueprint

We also need a way to interact with this character. For this, we are going to create a volume that expands a certain distance in front of the character; this will be the area in which the player is able to interact with the NPC. To do this, we will need to add another component to CapsuleComponent. Navigate to the Components panel and select Add Component | Collision | Box Collision:

Creating the NPC Character Blueprint

This creates a box collision at the character's origin:

Creating the NPC Character Blueprint

We are going to use this collision by calculating whether the player is inside the box or outside the box. If they are in the box, the player will be able to interact with the NPC; if the player is outside the box, the player will not be able to interact with the NPC. Since we want to make this as realistic as possible, the player should only interact with the NPC if the character is standing in front of the NPC. So adjust the location and scale of the box until it is in front of the character and sized such that the player can easily walk a forgivable distance in front of the NPC to interact with it.

For this character, I will edit the Location X value to be 60, Scale X value to be 2, Scale Y value to be 2, and Scale Z value to be 3:

Creating the NPC Character Blueprint

Finally, we want to give the collision box a type that does not block the player, but allows the player to enter the collision box. Navigate to Details | Collision and under Collision Presets, select Trigger:

Creating the NPC Character Blueprint

This will essentially turn the collision box into a trigger volume that can be used to trigger events such as dialog and shop menus.

At this point, you can drag and drop your NPC_ShopOwner into your level:

Creating the NPC Character Blueprint

If you playtest this, you should notice that you will collide with the skeletal mesh, but you will not collide with the trigger volume. You are now ready to create Blueprints to make this NPC interactive.

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

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