Roblox: Gamepad Input Cheat Sheet

A Games & Hobbies › Computer Games cheat sheet in English by Ozzypig

A quick reference for implementing gamepad (controller) input on Roblox. This cheatsheet is written for intermediate scripters.

31,784 views 6,085 downloads 28 Jan 2016 History
3.7 avg · 0 ratings

Contex­tAc­tio­nSe­rvice basics

Used for binding to inputs gracef­ully. If an input is bound to one action and another action is to use the same button, the BindAction and Unbind­Action functions will handle the collision properly. Example: (A) could make the player jump OR open a door but only when close enough.
Each input type bound to a function using BindAction works like a stack: whichever function was the most recent to be bound will be called when that input type is activated by the player.

Contex­tAc­tio­nSe­rvice binding

:BindAction(name, func, touchButton, inputTypes...)
Calls func with name, InputS­tate, and InputO­bject when inputTypes are used.
:UnbindAction(name)
Unbinds a function from action name.
Input types for :BindAction():
As soon as a player can use an input (like A/B, triggers, or thumbs­tick), use :BindA­cti­on(...). This will override the given input types current actions with the new one. When the player can no longer use the input for the action, use :Unbin­dAc­tio­n(name).

Contex­tAc­tio­nSe­rvice example

{{noshy}}
**local** cas = {{link="http://wiki.roblox.com/index.php?title=API:Class/ContextActionService"}}game:GetService("ContextActionService"){{/link}}
**function** handleAction(actionName, {{link="http://wiki.roblox.com/index.php?title=API:Enum/UserInputState"}}inputState{{/link}}, {{link="http://wiki.roblox.com/index.php?title=API:Class/InputObject"}}inputObject{{/link}})
   **if** actionName == "Swing sword" **and** inputState == {{link="http://wiki.roblox.com/index.php?title=API:Enum/UserInputState"}}Enum.UserInputState.Begin{{/link}} **then**
      print("Swinging sword")
   **end**
**end**

-- *When the sword is equipped:*
cas:{{link="http://wiki.roblox.com/index.php?title=API:Class/ContextActionService/BindAction"}}BindAction{{/link}}("Swing sword", handleAction, **false**, Enum.KeyCode.ButtonA)
-- *When the sword is unequipped:*
cas:{{link="http://wiki.roblox.com/index.php?title=API:Class/ContextActionService/UnbindAction"}}UnbindAction{{/link}}("Swing sword")
When the given input type is activa­ted­/ch­anged, the function passed to :BindA­cti­on(...) is called with the action name, the input state (Begin, Change, End or Cancel) and the InputO­bject. It's good practice to have just one action­-ha­ndling function per script.

Gamepad KeyCodes and UserIn­put­Types

Gamepad KeyCodes and UserInputTypes
Buttons use the UserIn­put­States Began/End. Triggers and thumbs­ticks use Change.

UserIn­put­Service querying

Property (boolean): GamepadEnabled
Returns true if at least one gamepad is connected.
Event: GamepadConnected
Fired when a gamepad is available.
Event:Gamepa­dDi­sco­nnected
Fired when a gamepad is no longer available.
Avoid using UserIn­put­Ser­vic­e.I­npu­tBegan for actions involving button presses! Use Contex­tAc­tio­nSe­rvi­ce:­Bin­dAc­tio­n(...) because this "­ove­rri­des­" existing actions using the given input types, and properly returns control to existing actions when yours are unbound.

Tips and Good Practices

A Button - Enum.K­eyC­ode.Bu­ttonA
Bound to jump by default. Should be used as accept button for prompts.
B Button - Enum.K­eyC­ode.Bu­ttonB
Should be used as back or cancel button for menus/­pro­mpts.
Right Trigger - Enum.K­eyC­ode.Bu­ttonR2
Use for primary character actions.
Left Trigger - Enum.K­eyC­ode.Bu­ttonL2
Use for secondary character actions.
Right Thumbstick - Enum.K­eyC­ode.Th­umb­stick1
Use for camera movement.
Left Thumbstick - Enum.K­eyC­ode.Th­umb­stick2
Use for character movement.
Right/Left Bumpers - Enum.K­eyC­ode.Bu­tto­nR1­/Bu­ttonL1
Bound to switch tools by default.
A good way to know what kinds of control schemes work is by playing other gamepa­d/c­ont­roller enabled games.

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 1

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.