Clase de motor
ContextActionService
*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.
Resumen
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 |
Muestras de código
Servicio de Acción del Contexto Recargar Herramienta
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recargar"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("¡Recargando!")
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)Referencia API
Métodos
BindAction
Devuelve
()
Muestras de código
Servicio de Acción del Contexto Recargar Herramienta
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recargar"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("¡Recargando!")
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)Manejador de Acción General
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Manejando acción: " .. actionName)
print(inputObj.UserInputType)
end
-- Dado que esta función no devuelve nada, este manejador
-- "hundirá" la entrada y no se llamarán otros manejadores de acción después
-- de este.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)Controladores de acción apilados
local ContextActionService = game:GetService("ContextActionService")
-- Define un controlador de acción para FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Controlador de Acción Uno: " .. actionName)
end
-- Este controlador de acción devuelve nil, por lo que se asume que
-- maneja la acción correctamente.
end
-- Vinculando la acción FirstAction (está en el fondo de la pila)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Define un controlador de acción para SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Controlador de Acción Dos: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Devolver nil implica que se hunde la entrada
return Enum.ContextActionResult.Sink
end
end
-- Vinculando SecondAction sobre la primera acción (dado que se vinculó más recientemente, está en la parte superior de la pila)
-- Nota que SecondAction utiliza las mismas teclas que
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)BindActionAtPriority
Devuelve
()
Muestras de código
Prioridades de BindAction de 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(`Acción [{actionName}] ocurrió. KeyCode [{inputObject.KeyCode}] presionado.`)
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(`Acción [{actionName}] ocurrió. KeyCode [{inputObject.KeyCode}] presionado.`)
return Enum.ContextActionResult.Sink
end
-- Sin especificar prioridad, la acción vinculada más recientemente se llama primero,
-- así que presionar INPUT_KEY1 imprime "Golpear" y luego hunde la entrada.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Aquí vinculamos ambas funciones en el mismo orden que arriba, pero con prioridades explícitamente intercambiadas.
-- Es decir, le damos a "Lanzar" una mayor prioridad de 2 para que se llame primero,
-- a pesar de que "Golpear" todavía esté vinculado más recientemente.
-- Presionar INPUT_KEY2 imprime "Lanzar" y luego hunde la entrada.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)BindActionToInputTypes
BindActivate
ContextActionService:BindActivate(
):()
Parámetros
Devuelve
()
GetBoundActionInfo
Parámetros
Devuelve
GetButton
SetDescription
Devuelve
()
Muestras de código
Botón táctil del servicio de acción contextual
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspeccionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagen de un globo de diálogo con ? en él
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspeccionando")
end
end
-- Para dispositivos táctiles, un botón se crea automáticamente en pantalla a través del 3er parámetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar estas funciones para personalizar el botón:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Mirar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspeccionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular el botón directamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Recuerda: ¡los dispositivos no táctiles no devolverán nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tintea el ImageButton de verde
endSetImage
Devuelve
()
Muestras de código
Botón táctil del servicio de acción contextual
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspeccionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagen de un globo de diálogo con ? en él
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspeccionando")
end
end
-- Para dispositivos táctiles, un botón se crea automáticamente en pantalla a través del 3er parámetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar estas funciones para personalizar el botón:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Mirar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspeccionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular el botón directamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Recuerda: ¡los dispositivos no táctiles no devolverán nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tintea el ImageButton de verde
endSetPosition
Devuelve
()
Muestras de código
Botón táctil del servicio de acción contextual
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspeccionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagen de un globo de diálogo con ? en él
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspeccionando")
end
end
-- Para dispositivos táctiles, un botón se crea automáticamente en pantalla a través del 3er parámetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar estas funciones para personalizar el botón:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Mirar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspeccionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular el botón directamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Recuerda: ¡los dispositivos no táctiles no devolverán nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tintea el ImageButton de verde
endSetTitle
Devuelve
()
Muestras de código
Botón táctil del servicio de acción contextual
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspeccionar"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Imagen de un globo de diálogo con ? en él
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspeccionando")
end
end
-- Para dispositivos táctiles, un botón se crea automáticamente en pantalla a través del 3er parámetro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Podemos usar estas funciones para personalizar el botón:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Mirar")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspeccionar algo.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Podemos manipular el botón directamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Recuerda: ¡los dispositivos no táctiles no devolverán nada!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tintea el ImageButton de verde
endUnbindAction
Parámetros
Devuelve
()
Muestras de código
Servicio de Acción del Contexto Recargar Herramienta
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recargar"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("¡Recargando!")
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 predeterminado: "Unknown" |
Devuelve
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
Devuelve
()
Eventos
LocalToolEquipped
Parámetros
LocalToolUnequipped
Parámetros