Classe motore
ContextActionService
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
Sommario
Metodi
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():() |
Eventi
LocalToolEquipped(toolEquipped: Instance):RBXScriptSignal |
LocalToolUnequipped(toolUnequipped: Instance):RBXScriptSignal |
Campioni di codice
Strumento di Ricarica del ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Ricarica"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Ricaricando!")
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)Riferimento API
Metodi
BindAction
Restituzioni
()
Campioni di codice
Strumento di Ricarica del ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Ricarica"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Ricaricando!")
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)Gestore Azioni Generali
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Gestione dell'azione: " .. actionName)
print(inputObj.UserInputType)
end
-- Poiché questa funzione non restituisce nulla, questo gestore
-- "assorbirà" l'input e non verranno chiamati altri gestori di azioni dopo
-- questo.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)Gestori di Azione Accumulati
local ContextActionService = game:GetService("ContextActionService")
-- Definisci un gestore di azioni per FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Gestore di Azioni Uno: " .. actionName)
end
-- Questo gestore di azioni restituisce nil, quindi si presume che
-- gestisca correttamente l'azione.
end
-- Collegamento dell'azione FirstAction (è in fondo allo stack)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Definisci un gestore di azioni per SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Gestore di Azioni Due: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Restituendo nil si affondano implicitamente gli input
return Enum.ContextActionResult.Sink
end
end
-- Collegamento di SecondAction sopra la prima azione (dato che è stata legata più di recente, è in cima allo stack)
-- Nota che SecondAction utilizza gli stessi tasti di
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)BindActionAtPriority
Restituzioni
()
Campioni di codice
Priorità di BindAction di 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(`Azione [{actionName}] avvenuta. KeyCode [{inputObject.KeyCode}] premuto.`)
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(`Azione [{actionName}] avvenuta. KeyCode [{inputObject.KeyCode}] premuto.`)
return Enum.ContextActionResult.Sink
end
-- Senza specificare la priorità, l'azione legata più di recente viene chiamata per prima,
-- quindi premendo INPUT_KEY1 stampa "Punch" e poi rallenta l'input.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Qui colleghiamo entrambe le funzioni nello stesso ordine di sopra, ma con priorità esplicitamente scambiate.
-- Cioè, diamo a "Throw" una priorità più alta di 2 affinché venga chiamata per prima,
-- nonostante "Punch" sia legata più recentemente.
-- Premendo INPUT_KEY2 stampa "Throw" e poi rallenta l'input.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)BindActionToInputTypes
BindActivate
ContextActionService:BindActivate(
):()
Parametri
Restituzioni
()
GetBoundActionInfo
Parametri
Restituzioni
GetButton
SetDescription
Restituzioni
()
Campioni di codice
Servizio di azione contestuale Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Ispeziona"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Immagine di un fumetto con ? dentro
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Ispezionando")
end
end
-- Per i dispositivi touch, un pulsante viene creato automaticamente sullo schermo tramite il 3° parametro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Possiamo usare queste funzioni per personalizzare il pulsante:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Guarda")
ContextActionService:SetDescription(ACTION_INSPECT, "Ispeziona qualcosa.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Possiamo manipolare il pulsante direttamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Ricorda: i dispositivi non touch non restituiranno nulla!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinteggia l'ImageButton di verde
endSetImage
Restituzioni
()
Campioni di codice
Servizio di azione contestuale Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Ispeziona"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Immagine di un fumetto con ? dentro
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Ispezionando")
end
end
-- Per i dispositivi touch, un pulsante viene creato automaticamente sullo schermo tramite il 3° parametro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Possiamo usare queste funzioni per personalizzare il pulsante:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Guarda")
ContextActionService:SetDescription(ACTION_INSPECT, "Ispeziona qualcosa.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Possiamo manipolare il pulsante direttamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Ricorda: i dispositivi non touch non restituiranno nulla!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinteggia l'ImageButton di verde
endSetPosition
Restituzioni
()
Campioni di codice
Servizio di azione contestuale Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Ispeziona"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Immagine di un fumetto con ? dentro
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Ispezionando")
end
end
-- Per i dispositivi touch, un pulsante viene creato automaticamente sullo schermo tramite il 3° parametro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Possiamo usare queste funzioni per personalizzare il pulsante:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Guarda")
ContextActionService:SetDescription(ACTION_INSPECT, "Ispeziona qualcosa.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Possiamo manipolare il pulsante direttamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Ricorda: i dispositivi non touch non restituiranno nulla!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinteggia l'ImageButton di verde
endSetTitle
Restituzioni
()
Campioni di codice
Servizio di azione contestuale Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Ispeziona"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Immagine di un fumetto con ? dentro
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Ispezionando")
end
end
-- Per i dispositivi touch, un pulsante viene creato automaticamente sullo schermo tramite il 3° parametro
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Possiamo usare queste funzioni per personalizzare il pulsante:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Guarda")
ContextActionService:SetDescription(ACTION_INSPECT, "Ispeziona qualcosa.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Possiamo manipolare il pulsante direttamente usando ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Ricorda: i dispositivi non touch non restituiranno nulla!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tinteggia l'ImageButton di verde
endUnbindAction
Parametri
Restituzioni
()
Campioni di codice
Strumento di Ricarica del ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Ricarica"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Ricaricando!")
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(
):()
Parametri
| Valore predefinito: "Unknown" |
Restituzioni
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
Restituzioni
()
Eventi
LocalToolEquipped
Parametri
LocalToolUnequipped
Parametri