Mapping Input in C++ with Unreal Engine 4

March 6, 2020

·

Mapping input for a project is easy enough to do through the Unreal Editor Project Settings menu, but sometimes it can be cumbersome to have to load up the Editor every time you want to change a setting when you are working in C++. Or you may want to map inputs quickly when working with Editor-free builds. Being able to remap inputs natively can speed up your iteration time considerably.

For information on how the Unreal Engine 4 input framework works, check out this article.

Even so, mapping inputs by directly editing config files has a few special use-cases, and it is error-prone compared to using the Project Settings menu. Use this method sparingly.

Creating an Input Config File

Your input mappings are contained in a config file called DefaultInput.ini. This file is located in the Config folder of your project. However, this file is only automatically created when you create a project from a template that contains input - the First Person Shooter Template and the Top Down Template are good examples. By contrast, "blank" projects will not have an input config file created automatically.

If you have already added input

Check that you have a file called DefaultInput.ini in the folder YourProjectFolder/Config/.

If you don't find the config file

Just add a placeholder input from the Editor. The Editor will generate the config file for you.To do so, follow these steps:

  1. Start your project in the Unreal Editor
  2. Open the Project Settings menu in the Edit toolbar at the top left of the screen
  3. Select the Input submenu from the options to the left, under the Engine heading
  4. Add an ActionMapping with any name you like, with any key binding you want
  5. Close the Project Settings menu
  6. The file DefaultInput.ini should now be created in the folder structure described above

You should now have a DefaultInput.ini file. The remainder of our work will take place in this file.

Though it is important that you understand where the input config file is located, you do not always need to navigate this folder structure to reach your config file. In fact, your project's Visual Studio solution (.sln) makes the Config folder available from the solution, which can often be the most convenient access point. You can also opt to edit the config file using an external editor. We use Visual Studio Code.

Adding Mappings

Ini files are separated into sections. Mappings belong under the section [/Script/Engine.InputSettings]

Action Mappings

Input Action mappings are discrete events that fire on a few specific occasions, such as when a button is pressed or released. A classic example is a Jump mapping to a spacebar press. We want to initiate a jump once when the spacebar is pressed and nothing more.

; Generic
[/Script/Engine.InputSettings]
+ActionMappings=(ActionName="YourActionNameHere",bShift=(boolean),bCtrl=boolean),bAlt=(boolean),bCmd=(boolean),Key=(PickAKey))

; Example
[/Script/Engine.InputSettings]
+ActionMappings=ActionName="Jump",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=SpaceBar) ;jump
+ActionMappings=ActionName="SuperJump",bShift=False,bCtrl=True,bAlt=False,bCmd=False,Key=SpaceBar) ;jump higher
  • ActionName is the identifier of the input mapping. You will have to match it exactly when you bind to it.
  • Key is the key that will trigger this action.
  • bShift, bCtrl, bAlt, and bCmd are your standard modifiers.

Axis Mappings

Input Axis mappings are continuous events that occur repeatedly so long as an input occurs, such as a when a button is held or a mouse is moved. A classic example is a MoveForward binding to W. We want to keep moving forward so long as W is pressed.

Another good example might be a helicopter - we keep applying thrust so long as the input is pressed.

; Generic
[/Script/Engine.InputSettings]
+AxisMappings=(AxisName="YourAxisNameHere",Scale=(float),Key=(PickAKey))

; Example
[/Script/Engine.InputSettings]
+AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=W) ; move forward+AxisMappings=(AxisName="MoveForward",Scale=-1.000000,Key=S) ; move backward
  • Axis Name is the identifier of the input mapping. You will have to match it exactly when you bind to it.
  • Key is the key that will trigger this axis.
  • Scale is the strength or direction of the axis.

You may notice that there are no modifiers (Shift/Ctrl/Cmd) for Axis mappings. Axis mappings are not concerned with modifiers. They are replaced by the Scale attribute, which allows you to have one mapping that can go a number of different ways.

The "MoveForward" mapping is a good example, as it comes with Engine templates. The W key gives a 1.0 scale and the S key gives a -1.0 scale. You can bind multiple inputs to the same axis mapping and simply pass different scale values to go forward or backward.

You can now add input mappings easily to your UE4 Project without ever leaving the (dis)comfort of Visual Studio. The next step is to bind your in-game responses to the mappings you set up.



© 2021 Mustafa Moiz.