Hello, I’ve seen a lot of games lately using Context Action Service instead of User Input Service. Should I be switching my game over to CAS? Also, I see that some say its easier to add mobile support with CAS but can’t you just write the same code that works for UIS function when a GUI is clicked?
Yes, ContextActionService (CAS) is preferred over UserInputService (UIS) in most cases. Here’s a short summary for both services. CAS is useful when you want a key to do something, like F to equip a flashlight, while UIS is more for like general input handling. By the way, it is easier to use CAS to make mobile buttons, as UIS isn’t so straightforward to make that UNLESS you can make it well-organized. Hope this helps!
CAS comes with built-in support for easily creating mobile buttons as @J_Angry mentioned. It also makes adding multiple inputs easier for different platforms. Instead of needing to connect UIS.InputBegan and UIS.InputEnded, you receive the state of the action in your handleAction function.
CAS also has the benefit of being easier to disconnect. Instead of adding conditional statements to multiple parts of your code, you can simply do CAS:UnbindAction(actionName)
.
The choice comes down to personal preference, if UIS is easier for you to implement then stick to that. CAS is known to be more complex since its primary responsibility is to add context to the existing UIS system; therefore, only use it if that sounds like what you need.
Example:
-- The function we call when our input gets updated
local function handleAction(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
local button = ContextActionService:GetButton("Action",)
button.image = ...
else
print("Input end")
end
end
-- (Action Name, Action Event Function, createTouchButton?, (inputs))
CAS:BindAction("Action", handleFunction, true, Enum.KeyCode.A, Enum.KeyCode.C, Enum.KeyCode.ButtonY)
This would have been nice to know when creating the gliding system for my game thanks for the explanation