Input Binding Basics
Input in the UDK is as simple as associating various input commands with exec functions that should be called when those inputs are triggered. Let's look at this in detail.
DefaultInput.ini
DefaultInput.ini is the configuration file where input bindings should be placed.
The name of the property responsible for associating a particular input with an exec function is Bindings. Let's look at an example:
.Bindings=(Name="F4",Command="Playersonly")
Note a few things:
- We're prefixing the Bindings keyword with the . operator which appends this line to the Bindings array when input.ini is built from the default configuration files
- We are associating the button F4 with the exec function Playersonly such that when F4 is pressed, the function Playersonly is called
- Note that the function is not continuously called, it is called once per keydown event
The full list of inputs that could be associated is on the UDN here.
In the simplest use case, this is adequate and will fulfill most of your requirements. You can do a few more tricks, however.
Binding to Member Variables
It is also possible to associate inputs with member variables directly. To do this, we define a target input variable on a class that derives from Controller or Input; typically, this would be done either on a subclass of PlayerController or PlayerInput.
For example, assume that the following variable is defined on a subclass of the PlayerInput class:
var input byte isPressing;
Note use of the special variable qualifier input. This allows the input system to set the value of this variable directly, if later referenced in DefaultInput.ini.
Let's look at how we could use this in DefaultInput.ini:
.Bindings=(Name="LeftMouseButton",Command="Button isPressing")
The system will now set the value of isPressing to 1 if the left mouse button is currently pressed, and 0 otherwise. Also notice use of the command qualifier Button. Let's look at qualifiers like this in detail.
Command Qualifier: Axis
Axis sets a float variable usually within the range -1.0f and 1.0f. The main exception to this is the mouse input axes. Let's look at an example of Axis in action:
For example, assume the following variable is defined on a subclass of the PlayerInput class:
var input float mouseYAxis;
We can then use this variable like this:
.Bindings=(Name="MouseY",Command="Axis mouseYAxis")
mouseYAxis will now hold the number of pixels that the mouse has moved on the Y axis in the last frame. You can learn more about axis on the UDN here
Command Qualifier: Button
Button sets a byte variable to either 0 or 1, depending on whether the key is currently released or pressed, respectively.
For example, assume the following variable is defined on a subclass of the PlayerInput class:
var input byte f3Pressed;
We can then use this variable like this:
.Bindings=(Name="F3",Command="Button f3Pressed")
f3Pressed will now be set to 1 if the button F3 is currently pressed, and 0 otherwise.
Command Qualifier: Count
Contrary to how this qualifier is named, this does not create an input mapping that increments a variable. Instead, the number of samples of the particular input in the last frame is reported; that is, how many times in the last frame the input mapping was queried. This is useful for things like mouse smoothing.
Count operates on byte variables.
For example, assume the following variable is defined on a subclass of the PlayerInput class:
var input byte mouseXSamples;
We can then use this variable like this:
.Bindings=(Name="MouseX",Command="Count mouseXSamples")
Command Qualifier: Toggle
Toggle flips a byte from 0 to 1 or from 1 to 0 every time the associated input is pressed down.
For example, assume the following variable is defined on a subclass of the PlayerInput class:
var input byte MState;
We can then use this variable like this:
.Bindings=(Name="M",Command="Toggle MState")