Engine-klasse
ContextActionService
*Deze inhoud is vertaald met behulp van AI (bèta) en kan fouten bevatten. Klik hier om deze pagina in het Engels te bekijken.
Samenvatting
Methoden
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():() |
Evenementen
LocalToolEquipped(toolEquipped: Instance):RBXScriptSignal |
LocalToolUnequipped(toolUnequipped: Instance):RBXScriptSignal |
Codevoorbeelden
ContextActionService Tool Herladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Herladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Herladen!")
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)API-referentie
Methoden
BindAction
Teruggaves
()
Codevoorbeelden
ContextActionService Tool Herladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Herladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Herladen!")
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)Algemene Actiehandler
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Actie verwerken: " .. actionName)
print(inputObj.UserInputType)
end
-- Aangezien deze functie niets retourneert, zal deze handler
-- de invoer "sink" en zullen geen andere actiehandlers na
-- deze worden aangeroepen.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)Gestapelde Actiehandlers
local ContextActionService = game:GetService("ContextActionService")
-- Definieer een actiehandler voor FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Actiehandler Eén: " .. actionName)
end
-- Deze actiehandler retourneert nil, dus het wordt aangenomen dat
-- het de actie correct afhandelt.
end
-- Binding van de actie FirstAction (het staat onderaan de stapel)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Definieer een actiehandler voor SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Actiehandler Twee: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Nil retourneren onderdrukt invoeren impliciet
return Enum.ContextActionResult.Sink
end
end
-- Binding van SecondAction boven de eerste actie (aangezien het recentelijker is gebonden, staat het bovenaan de stapel)
-- Let op dat SecondAction dezelfde toetsen gebruikt als
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)BindActionAtPriority
Teruggaves
()
Codevoorbeelden
ContextActionService ActieBinden Prioriteiten
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(`Actie [{actionName}] uitgevoerd. KeyCode [{inputObject.KeyCode}] ingedrukt.`)
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(`Actie [{actionName}] uitgevoerd. KeyCode [{inputObject.KeyCode}] ingedrukt.`)
return Enum.ContextActionResult.Sink
end
-- Zonder prioriteit op te geven, wordt de meest recent gebonden actie als eerste aangeroepen,
-- dus het indrukken van INPUT_KEY1 print "Punch" en sinkt vervolgens de invoer.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Hier binden we beide functies in dezelfde volgorde als hierboven, maar met expliciet verwisselde prioriteiten.
-- Dat wil zeggen, we geven "Throw" een hogere prioriteit van 2 zodat deze als eerste wordt aangeroepen,
-- ondanks dat "Punch" nog steeds recentelijker is gebonden.
-- Het indrukken van INPUT_KEY2 print "Throw" en sinkt vervolgens de invoer.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)BindActionToInputTypes
BindActivate
ContextActionService:BindActivate(
):()
Parameters
Teruggaves
()
GetBoundActionInfo
Parameters
Teruggaves
GetButton
SetDescription
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
endSetImage
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
endSetPosition
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
endSetTitle
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
endUnbindAction
Parameters
Teruggaves
()
Codevoorbeelden
ContextActionService Tool Herladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Herladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Herladen!")
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(
):()
Parameters
| Standaardwaarde: None |
Teruggaves
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
Teruggaves
()
Evenementen
LocalToolEquipped
Parameters
LocalToolUnequipped
Parameters