In my last post I made, I asked how to use UIS only is spefic situations and the majority said to use CAS instead. I have been struggling to find resources on the topic of situational CAS so I would be very grateful if someone could explain to me how to use it, and possibly break-down ROBLOX’s provided sample.
Thanks, pevdn
ROBLOX’s provided sample:
local ContextActionService = game:GetService("ContextActionService")
-- A car horn sound
local honkSound = Instance.new("Sound", workspace)
honkSound.Looped = true
honkSound.SoundId = "rbxassetid://3017580236"
local function handleAction(actionName, inputState, inputObject)
if actionName == "HonkHorn" then
if inputState == Enum.UserInputState.Begin then
honkSound:Play()
else
honkSound:Pause()
end
end
end
-- When the player sits in the vehicle:
ContextActionService:BindAction("HonkHorn", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
-- When the player gets out:
ContextActionService:UnbindAction("HonkHorn")
For the reasons why you gotta compare it with user input services which is what I have done in the post below
The primary reason is dealing with key bindings as CAS makes it easier to unbind rather than UIS where you need to disconnect the connection or use a bool value.
For now I don’t have know a specific case example where user input services would be better than CAS.
Perhaps when you just want to permanently bind a key or always listen to an input.
ContextActionService is atrocious to work with in my opinion. From my experience you’d really only want to use it if you can’t do an InputBegan on something because Roblox has priority control over it (for example scrolling or the I/O keys already being in use by the camera, or wasd/arrows being in use by the default controls)
So you and future developers don’t have to suffer like I (and many others) had to, here’s some cut down example code from a custom camera I made for my game. I commented it a bunch to try and give you a rough idea of what it’s doing. Hopefully it helps at least a little bit.
Example code
local contextActionService = game:GetService("ContextActionService")
local function zoomControl(name,state,input) -- Function that gets called when CAS does stuff
if name == "zooming" then -- Mouse scrollwheel control
if input.Position.Z == 1 then -- Scroll up
if camheight - camChange >= minHeight then
camheight = camheight - camChange
end
elseif input.Position.Z == -1 then -- Scroll down
if camheight + camChange <= maxHeight then
camheight = camheight + camChange
end
end
elseif name == "zoomIn" then -- I key
if camheight - camChange >= minHeight then
camheight = camheight - camChange
end
elseif name == "zoomOut" then -- O key
if camheight + camChange <= maxHeight then
camheight = camheight + camChange
end
end
end
contextActionService:BindAction("zooming", zoomControl, false, Enum.UserInputType.MouseWheel)
contextActionService:BindAction("zoomIn", zoomControl, false, Enum.KeyCode.I)
contextActionService:BindAction("zoomOut", zoomControl, false, Enum.KeyCode.O)
-- Name that the function refers to, which function gets called, no idea what true/false does but it's called state in the arguments, and which input is going to call this function (most often a keycode for keyboard input)
Note: It doesn’t matter what names you give the actions, as long as the function refers to the action name
Edit: Also I believe it only works in local scripts because it’s listening to local inputs.
It being atrocious is entirely my opinion, for all I know it could be the greatest thing to exist in Roblox scripting.
For the others suffering part however, there are many threads under Topics tagged contextactionservice so I’m clearly not the only one having issues on working with it.