PlayerInput
A PlayerInput class is used to encapsulate that part of a player controller that receives input from the input devices currently attached. Typically, it contains the following:
- Variables to track certain input values (ie whether player should be ducking, how much the mouse has moved in the last tick, etc...)
- Exec functions called directly from DefaultInput.ini that update specific values as required
- An override of the function PlayerInput, which returns nothing and accepts float deltatime
- This function should perform any time-related updates to the input object or reset any variables that need resetting as required
Association With PlayerController
PlayerController has code for automatically constructing and storing the associated PlayerInput object. This is done via two variables:
var class<PlayerInput> InputClass
- This variable holds the class of PlayerInput that this PlayerController should construct. This is defined in the defaultproperties of PlayerController classes
- UTPlayerController assigns the class UTPlayerInput to this
var PlayerInput PlayerInput
- A wonderfully named variable that stores this player controller's current player input object. Any objects wishing to gain access to the player controller's player input object should access it via this variable
Outer Actor
PlayerInput is not an Actor. However, it is instantiated by the PlayerController Actor. Upon instantiation, that player controller instance is set up as an outer for this object. So, this object has access to all of the members of the containing player controller, at least all of the members defined in the class PlayerController, not any subclass of it; to gain access to members defined in more derived classes, you should cast the Outer variable present on the PlayerInput object.
This can be used to gain access to the PlayerController's Pawn (through the Pawn member) or HUD (through the myHUD member).
Creating Custom PlayerInput Class
If creating a new player input class, it is recommended that you extend an existing one. One such useful PlayerInput class is UTPlayerInput (PlayerInput itself defines a lot of functionality, use whichever is suitable to your needs). This will give you access to the following variables that are maintained by that class's PlayerInput() function:
Axial Variables
- aMouseX, aMouseY
- How many pixels the mouse moved in the last tick. This number may be too large to be useful and may need to be scaled.
- aForward, aStrafe
- How many units the player wants to move forward or left/right. The exact meaning of the units in this case is unclear. Take a look at DefaultInput.ini to see how these are updated and decide for yourself how you would use them.
- aTurn, aUp
- How many units the player wants to turn their view left/right or up/down. Similar issue to above.
Take a look at PlayerInput.uc for many more of these and look at DefaultInput.ini to see how they are bound to various input devices.
Using Custom PlayerInput Class
Using a custom-defined PlayerInput class is as simple as setting the custom player input class as the default value of a player controller's InputClass variable.