Reader small image

You're reading from  Game Development Projects with Unreal Engine

Product typeBook
Published inNov 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781800209220
Edition1st Edition
Languages
Tools
Right arrow
Authors (4):
Hammad Fozi
Hammad Fozi
author image
Hammad Fozi

Hammad Fozi comes from a gaming background and has been extensively working on Unreal Engine since 2017. He has been part of some very successful AAA projects such as Virtua FanCave (and Metaverse), Unnamed AAA Sci-Fi DJ Experience, Heroes and Generals, and Creed: Rise to Glory VR. Hammad has worked with teams who have had experience working at Ubisoft, Warner Bros. Games, 2K Games, and more! He has successfully helped teams consisting of 10–30 people to scale to 150+ in size over his very short yet impressive career. Hammad currently works as a senior C++ game developer and has extensive experience in working with VR and augmented reality, PC/PS5/Xbox/Android/iOS/macOS game development, and Web3/Metaverse/NFT systems (within Unreal Engine).
Read more about Hammad Fozi

Gonçalo Marques
Gonçalo Marques
author image
Gonçalo Marques

Gonçalo Marques has been an active gamer since the age of 6. He has been using Unreal Engine since 2016 and has done freelance and consulting work using the engine. Gonçalo also released a free and open source plugin called UI Navigation, which has garnered an extremely positive reception with over 100,000 downloads and is still receiving frequent updates and fixes. Thanks to the development of this plugin, he became an Epic MegaGrant recipient. He is now working at Funcom ZPX, a game studio in Lisbon that has contributed to games such as Conan Exiles, Mutant Year Zero, and Moons of Madness. Gonçalo is currently working on a new Funcom game in the Dune universe.
Read more about Gonçalo Marques

David Pereira
David Pereira
author image
David Pereira

David Pereira started making games in 1998 when he learned how to use Clickteam's The Games Factory. He graduated in computer science from FCT-UNL, where he learned about C++, OpenGL, and DirectX, which allowed him to create more complex games. After working in IT consulting for a few years, he joined Miniclip in Portugal where he worked on popular mobile games such as 8 Ball Pool, Gravity Guy 1 and Gravity Guy 2, Extreme Skater, iStunt2, Hambo, and many others. Since then, he has been the lead developer for MPC in the John Lewis Christmas VR Experience, worked on an earlier version of Mortal Shell, and did volunteer work teaching people with Asperger's how to make games with Unreal Engine 4. Today, he's working on his own game, a soon-to-be-announced first-person action RPG.
Read more about David Pereira

Devin Sherry
Devin Sherry
author image
Devin Sherry

Devin Sherry is originally from Levittown, NY, located on Long Island. He studied the topics of Game Development and Game Design at the University of Advancing Technology where he had earned his Bachelor of Arts in Game Design in 2012. During his time in college, Devin worked as a game and level designer with a group of students called Autonomous Games on a real-time strategy styled, third-person shooter called The Afflicted using Unreal Engine 3/UDK where it was presented at GDC in 2013 at the GDC Play Showcase. Today, Devin works as an independent game developer located in Tempe, Arizona, where he works on personal and contracted projects. His achievements include the title Radial Impact, which can be found in the Community Contributions section of the Learn Tab of Unreal Engine 4's Launcher, and his work on his YouTube Channel, Devin Level Design, where he educates viewers on game development within Unreal Engine 3, UDK, and Unreal Engine 4.
Read more about Devin Sherry

View More author details
Right arrow

4. Player Input

Overview

This chapter will tackle the subject of player input. We will learn how to associate a keypress or touch input from a touch-enabled device with an in-game action such as jumping or moving.

By the end of this chapter, you will know about Action Mappings and Axis Mappings, how to create and modify them, how to listen to each of those mappings, how to execute in-game actions when they're pressed and released, and how to preview your game as if you were playing on a mobile device.

Introduction

In the previous chapter, we created our C++ class that inherits from the Character class and added all the necessary Actor components to be able to see the game from that character's perspective, as well as being able to see the character itself. We then created a Blueprint class that inherits from that C++ class, in order to visually set up all its necessary components. We also learned briefly about Action and Axis Mappings.

In this chapter, we will be going more in-depth on these topics, as well as covering their C++ usage. We will learn about how player input works in UE4, how the engine handles input events (key presses and releases), and how we can use them to control logic in our game.

Let's start this chapter by getting to know how UE4 abstracts the keys pressed by the player to make it easier for you to be notified of those events.

Note

In this chapter, we will be using an alternative version of the Character blueprint we created, called...

Input Actions and Axes

Player input is the thing that distinguishes video games from other entertainment media: the fact that they're interactive. For a video game to be interactive, it must take into account the player's input. Many games do this by allowing the player to control a virtual character that acts upon the virtual world it's in, depending on the keys and buttons that the player presses, which is exactly what we'll be doing in this chapter.

Most game development tools nowadays allow you to abstract keypresses into Actions and Axes, which allow you to associate a name (for example, Jump) with several different player inputs (pressing a button, flicking a thumbstick, and so on). The difference between Actions and Axes is that Actions are used for binary inputs (inputs that can either be pressed or released, like the keys on the keyboard), while Axes are used for inputs that are scalar or continuous (that is, that can have a range of values, like...

Processing Player Input

Let's think about a situation where the player presses the Jump action, which is associated with the Spacebar key, to get the player character to jump. Between the moment the player presses the Spacebar key and the moment the game makes the player character jump, quite a few things have to connect those two events.

Let's take a look at all the steps necessary that lead from one event to the other:

  1. Hardware Input: The player presses the Spacebar key. UE4 will be listening to this keypress event.
  2. The PlayerInput class: After the key is pressed or released, this class will translate that key into an action or axis. If there is an action or axis associated with that key, it will notify all classes that are listening to the action that it was just pressed, released, or updated. In this case, it will know that the Spacebar key is associated with the Jump action.
  3. The Player Controller class: This is the first class to receive these events...

DefaultInput.ini

If you go to your project's directory, using File Explorer, and then open its Config folder, you'll find some .ini files in it, one of which should be the DefaultInput.ini file. As the name suggests, this file holds the main settings and configuration for input-related properties.

In the first exercise of this chapter, where we edited the project's Input settings, what was happening, in reality, was that the editor was writing to and reading from the DefaultInput.ini file.

Open this file in a text editor of your choice. It contains many properties, but the ones we want to take a look at now are the list of Action Mappings and Axis Mappings. Near the end of the file, you should see, for instance, the Jump action being specified in this file:

+ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,  bAlt=False,bCmd=False,Key=SpaceBar)
+ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,  bAlt=False,bCmd=False,Key...

Turning the camera around the character

Cameras are an extremely important part of games, as they dictate what and how the player will see your game throughout the play session. When it comes to third-person games, which is the case for this project, the camera allows you not only to see the world around them but also the character you're controlling. Whether the character is taking damage, falling, or something else, it's important for the player to always know the state of the character they are controlling and to be able to have the camera face the direction they choose.

Much like with every modern, third-person game, we will always have the camera rotate around our player character. To have our camera rotate around our character, after setting up the Camera and Spring Arm components in Chapter 2, Working with Unreal Engine, let's continue by adding two new Axis Mappings, the first one called Turn, which is associated with the Gamepad Right Thumbstick X-Axis and...

Summary

In this chapter, you've learned how to add, remove, and modify Action Mappings and Axis Mappings, which give you some flexibility when determining which keys trigger a specific action or axis, how to listen to them, and how to execute in-game logic when they're pressed and released.

Now that you know how to handle the player's input, you can allow the player to interact with your game and offer the agency that video games are so well known for.

In the next chapter, we'll start making our own game from scratch. It'll be called Dodgeball and will consist of the player controlling a character trying to run away from enemies that are throwing dodgeballs at it. In that chapter, we will have the opportunity to start learning about many important topics, with a heavy focus on collisions.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Game Development Projects with Unreal Engine
Published in: Nov 2020Publisher: PacktISBN-13: 9781800209220
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.
undefined
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 €14.99/month. Cancel anytime

Authors (4)

author image
Hammad Fozi

Hammad Fozi comes from a gaming background and has been extensively working on Unreal Engine since 2017. He has been part of some very successful AAA projects such as Virtua FanCave (and Metaverse), Unnamed AAA Sci-Fi DJ Experience, Heroes and Generals, and Creed: Rise to Glory VR. Hammad has worked with teams who have had experience working at Ubisoft, Warner Bros. Games, 2K Games, and more! He has successfully helped teams consisting of 10–30 people to scale to 150+ in size over his very short yet impressive career. Hammad currently works as a senior C++ game developer and has extensive experience in working with VR and augmented reality, PC/PS5/Xbox/Android/iOS/macOS game development, and Web3/Metaverse/NFT systems (within Unreal Engine).
Read more about Hammad Fozi

author image
Gonçalo Marques

Gonçalo Marques has been an active gamer since the age of 6. He has been using Unreal Engine since 2016 and has done freelance and consulting work using the engine. Gonçalo also released a free and open source plugin called UI Navigation, which has garnered an extremely positive reception with over 100,000 downloads and is still receiving frequent updates and fixes. Thanks to the development of this plugin, he became an Epic MegaGrant recipient. He is now working at Funcom ZPX, a game studio in Lisbon that has contributed to games such as Conan Exiles, Mutant Year Zero, and Moons of Madness. Gonçalo is currently working on a new Funcom game in the Dune universe.
Read more about Gonçalo Marques

author image
David Pereira

David Pereira started making games in 1998 when he learned how to use Clickteam's The Games Factory. He graduated in computer science from FCT-UNL, where he learned about C++, OpenGL, and DirectX, which allowed him to create more complex games. After working in IT consulting for a few years, he joined Miniclip in Portugal where he worked on popular mobile games such as 8 Ball Pool, Gravity Guy 1 and Gravity Guy 2, Extreme Skater, iStunt2, Hambo, and many others. Since then, he has been the lead developer for MPC in the John Lewis Christmas VR Experience, worked on an earlier version of Mortal Shell, and did volunteer work teaching people with Asperger's how to make games with Unreal Engine 4. Today, he's working on his own game, a soon-to-be-announced first-person action RPG.
Read more about David Pereira

author image
Devin Sherry

Devin Sherry is originally from Levittown, NY, located on Long Island. He studied the topics of Game Development and Game Design at the University of Advancing Technology where he had earned his Bachelor of Arts in Game Design in 2012. During his time in college, Devin worked as a game and level designer with a group of students called Autonomous Games on a real-time strategy styled, third-person shooter called The Afflicted using Unreal Engine 3/UDK where it was presented at GDC in 2013 at the GDC Play Showcase. Today, Devin works as an independent game developer located in Tempe, Arizona, where he works on personal and contracted projects. His achievements include the title Radial Impact, which can be found in the Community Contributions section of the Learn Tab of Unreal Engine 4's Launcher, and his work on his YouTube Channel, Devin Level Design, where he educates viewers on game development within Unreal Engine 3, UDK, and Unreal Engine 4.
Read more about Devin Sherry