A quick reference for implementing gamepad (controller) input on Roblox. This cheatsheet is written for intermediate scripters.
:BindAction(name, func, touchButton, inputTypes...)
Calls
func with name, InputState, and InputObject when inputTypes are used.
|
:UnbindAction(name)
Unbinds a function from action
name.
|
|
Input types for
:BindAction(): |
:BindAction(...). 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 :UnbindAction(name).
{{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")
|
Guides and Tutorials
|
|
Game Services
|
|
Enum Types
|
|
Property (boolean):
GamepadEnabled
Returns true if at least one gamepad is connected.
|
|
Event:
GamepadConnected
Fired when a gamepad is available.
|
|
Event:
GamepadDisconnected
Fired when a gamepad is no longer available.
|
UserInputService.InputBegan for actions involving button presses! Use ContextActionService:BindAction(...) because this "overrides" existing actions using the given input types, and properly returns control to existing actions when yours are unbound.
|
A Button - Enum.KeyCode.ButtonA
Bound to jump by default. Should be used as accept button for prompts.
|
|
B Button - Enum.KeyCode.ButtonB
Should be used as back or cancel button for menus/prompts.
|
|
Right Trigger - Enum.KeyCode.ButtonR2
Use for primary character actions.
|
|
Left Trigger - Enum.KeyCode.ButtonL2
Use for secondary character actions.
|
|
Right Thumbstick - Enum.KeyCode.Thumbstick1
Use for camera movement.
|
|
Left Thumbstick - Enum.KeyCode.Thumbstick2
Use for character movement.
|
|
Right/Left Bumpers - Enum.KeyCode.ButtonR1/ButtonL1
Bound to switch tools by default.
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.