Classe do mecanismo
ContextActionService
*Este conteúdo é traduzido por IA (Beta) e pode conter erros. Para ver a página em inglês, clique aqui.
Resumo
Métodos
BindAction(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):() |
BindActionToInputTypes(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):() |
BindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodesForActivation: Tuple):() |
GetBoundActionInfo(actionName: string):Dictionary |
SetDescription(actionName: string,description: string):() |
SetPosition(actionName: string,position: UDim2):() |
UnbindAction(actionName: string):() |
UnbindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodeForActivation: Enum.KeyCode):() |
UnbindAllActions():() |
Eventos
LocalToolEquipped(toolEquipped: Instance):RBXScriptSignal |
LocalToolUnequipped(toolUnequipped: Instance):RBXScriptSignal |
Amostras de código
Serviço de Ação de Contexto Ferramenta Recarregar
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recarregar"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Recarregando!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)Referência API
Métodos
BindAction
Devolução
()
Amostras de código
Serviço de Ação de Contexto Ferramenta Recarregar
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recarregar"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Recarregando!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)Manipulador de Ação Geral
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Manipulando ação: " .. actionName)
print(inputObj.UserInputType)
end
-- Como esta função não retorna nada, este manipulador irá
-- "afundar" a entrada e nenhum outro manipulador de ação será chamado depois
-- deste.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)Manipuladores de Ação Empilhados
local ContextActionService = game:GetService("ContextActionService")
-- Defina um manipulador de ação para FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Manipulador de Ação Um: " .. actionName)
end
-- Este manipulador de ação retorna nil, então presume-se que
-- ele lida corretamente com a ação.
end
-- Vinculando a ação FirstAction (está na base da pilha)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Defina um manipulador de ação para SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Manipulador de Ação Dois: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Retornando nil implicitamente afunda os inputs
return Enum.ContextActionResult.Sink
end
end
-- Vinculando SecondAction sobre a primeira ação (já que ela foi vinculada mais recentemente, está no topo da pilha)
-- Note que SecondAction usa as mesmas teclas que
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)BindActionAtPriority
Devolução
()
Amostras de código
Prioridades de BindAction do ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local INPUT_KEY1 = Enum.KeyCode.Q
local INPUT_KEY2 = Enum.KeyCode.E
local function handleThrow(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Ação [{actionName}] ocorreu. KeyCode [{inputObject.KeyCode}] pressionado.`)
return Enum.ContextActionResult.Sink
end
local function handlePunch(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Ação [{actionName}] ocorreu. KeyCode [{inputObject.KeyCode}] pressionado.`)
return Enum.ContextActionResult.Sink
end
-- Sem especificar prioridade, a ação vinculada mais recentemente é chamada primeiro,
-- então pressionar INPUT_KEY1 imprime "Soco" e depois absorve a entrada.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Aqui vinculamos ambas as funções na mesma ordem que acima, mas com prioridades explicitamente trocadas.
-- Ou seja, damos a "Lançar" uma prioridade maior de 2 para que seja chamada primeiro,
-- apesar de "Soco" ainda estar vinculado mais recentemente.
-- Pressionar INPUT_KEY2 imprime "Lançar" e então absorve a entrada.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)BindActionToInputTypes
BindActivate
ContextActionService:BindActivate(
):()
Parâmetros
Devolução
()
GetBoundActionInfo
Parâmetros
Devolução
GetButton
SetDescription
Devolução
()
Amostras de código
Serviço de Ação de Contexto Botão de Toque
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagem de uma bolha de fala com ? nela
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecionando")
end
end
-- Para dispositivos de toque, um botão é criado na tela automaticamente via o 3º parâmetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar essas funções para personalizar o botão:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Olhar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular o botão diretamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Lembre-se: dispositivos que não são de toque não retornarão nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinge o ImageButton de verde
endSetImage
Devolução
()
Amostras de código
Serviço de Ação de Contexto Botão de Toque
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagem de uma bolha de fala com ? nela
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecionando")
end
end
-- Para dispositivos de toque, um botão é criado na tela automaticamente via o 3º parâmetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar essas funções para personalizar o botão:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Olhar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular o botão diretamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Lembre-se: dispositivos que não são de toque não retornarão nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinge o ImageButton de verde
endSetPosition
Devolução
()
Amostras de código
Serviço de Ação de Contexto Botão de Toque
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagem de uma bolha de fala com ? nela
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecionando")
end
end
-- Para dispositivos de toque, um botão é criado na tela automaticamente via o 3º parâmetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar essas funções para personalizar o botão:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Olhar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular o botão diretamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Lembre-se: dispositivos que não são de toque não retornarão nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinge o ImageButton de verde
endSetTitle
Devolução
()
Amostras de código
Serviço de Ação de Contexto Botão de Toque
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagem de uma bolha de fala com ? nela
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecionando")
end
end
-- Para dispositivos de toque, um botão é criado na tela automaticamente via o 3º parâmetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar essas funções para personalizar o botão:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Olhar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular o botão diretamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Lembre-se: dispositivos que não são de toque não retornarão nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinge o ImageButton de verde
endUnbindAction
Parâmetros
Devolução
()
Amostras de código
Serviço de Ação de Contexto Ferramenta Recarregar
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recarregar"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Recarregando!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)UnbindActivate
ContextActionService:UnbindActivate(
):()
Parâmetros
| Valor Padrão: "Unknown" |
Devolução
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
Devolução
()
Eventos
LocalToolEquipped
Parâmetros
LocalToolUnequipped
Parâmetros