ContextActionService

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
บริการ

อนุญาตให้ประสบการณ์ผูกการใส่ข้อมูลผู้ใช้กับการดำเนินการตามบริบทหรือการดำเนินการที่เปิดใช้งานเฉพาะภายใต้เงื่อนไขหรือระยะเวลาบางอย่างตัวอย่างเช่น อนุญาตให้ผู้เล่นเปิดประตูเฉพาะในขณะที่อยู่ใกล้เท่านั้นในรหัสการดำเนินการเป็นเพียงสตริง (แอ็คชัน) ที่ใช้โดยบริการเพื่อแยกแยะระหว่างการดำเนินการที่ไม่ซ้ำกันสตริงการดำเนินการจะถูกจัดให้กับ BindAction และ UnbindAction ในหมู่ฟังก์ชันสมาชิกอื่น ๆหากการกระทำสองอย่างมีการผูกกับอินพุตเดียวกัน อินพุตล่าสุดที่ผูกไว้จะได้รับความสำคัญเมื่อการดำเนินการล่าสุดถูกยกเลิกการผูก การผูกก่อนหน้านั้นจะควบคุมอีกครั้งเนื่องจากบริการนี้จัดการกับการป้อนของผู้ใช้คุณสามารถใช้ได้เฉพาะในด้านไคลเอนต์ LocalScripts

บริบทและการดำเนินการ

บริบท แอ็คชันเป็นเพียงช่วงเวลาที่ผู้เล่นสามารถดำเนินการได้ตัวอย่างบางส่วนรวมถึงการถือ Tool , อยู่ seated ในรถหรือยืนอยู่ใกล้ประตูไม่ว่ากรณีจะเป็นอย่างไรก็ตาม ขึ้นอยู่กับคุณ LocalScripts ที่จะโทร BindAction เมื่อบริบทถูกใส่และ UnbindAction เมื่อบริบทถูกทิ้ง

การดำเนินการ จะเป็นเพียงบางอย่างที่สามารถดำเนินการได้โดยผู้เล่นในขณะที่อยู่ในบริบทนั้น การดำเนินการดังกล่าวสามารถเปิด/ปิดเมนูบางอย่าง เรียกการดำเนินการเครื่องมือรองหรือส่งคำขอไปยังเซิร์ฟเวอร์โดยใช้ RemoteFunction:InvokeServer()การดำเนินการจะถูกระบุโดยสตริงที่ไม่ซ้ำกันเป็นพารามิเตอร์แรกของทั้ง BindAction และ UnbindActionสตริงสามารถเป็นอะไรก็ได้ แต่มันควรสะท้อนถึงการดำเนินการ ไม่ใช่การใช้อินพุตที่ใช้อยู่ ตัวอย่างเช่น อย่าใช้ "KeyH" เป็นชื่อการดำเนินการ - ใช้ "CarHorn" แทนควรกำหนดการดำเนินการของคุณเป็นคงที่ที่ด้านบนของสคริปต์เนื่องจากคุณจะใช้มันในสถานที่ที่แตกต่างกันอย่างน้อยสามแห่งในรหัสของคุณ

ผูกการดำเนินการในบริบทได้

ดีกว่าที่จะใช้ ContextActionService's BindAction มากกว่า UserInputService.InputBegan สำหรับส่วนใหญ่กรณีสำหรับ UserInputService.InputBegan ฟังก์ชันที่เชื่อมต่อของคุณจะต้องตรวจสอบว่าผู้เล่นอยู่ในบริบทของการดำเนินการที่ดำเนินการอยู่ในกรณีส่วนใหญ่ นี่ยากกว่าการเรียกฟังก์ชันเมื่อบริบทถูกใส่/ออกตัวอย่างเช่น หากคุณต้องการให้แป้น H กดเสียงแตรรถในขณะที่ผู้เล่นนั่งอยู่ในนั้น ผู้เล่นอาจพิมพ์ "สวัสดี" ในแชทหรือใช้แป้น H สําหรับสิ่งอื่นยากที่จะตรวจสอบว่ามีบางอย่างอื่นใช้กุญแจ H (เช่นแชท) - รถอาจจะแตรเมื่อผู้เล่นไม่ได้ตั้งใจหากคุณใช้ BindAction และ UnbindAction เมื่อผู้เล่นเข้า/ออกจากรถ ContextActionService จะตรวจสอบให้แน่ใจว่าการกดปุ่ม H แอ็คชัน(เช่นการแชท) ได้รับการควบคุมคุณจะไม่ต้องกังวลเกี่ยวกับการตรวจสอบนั้น

ตรวจสอบการดำเนินการที่ผูก

เพื่อดูรายการการดำเนินการและอินพุตที่ผูกไว้ คุณสามารถตรวจสอบแท็บ "การผูกการกระทำ" ในคอนโซลนักพัฒนา (F9 ในขณะที่อยู่ในเกม)สิ่งนี้แสดงการผูกทั้งหมดรวมถึงการผูกโดยสคริปต์หลักของ Roblox และสคริปต์กล้อง/การควบคุมเริ่มต้นด้วยนี่มีประโยชน์สำหรับการแก้ไขข้อผิดพลาดหากการดำเนินการของคุณถูกผูก/ถอดออกในเวลาที่ถูกต้องหรือหากการดำเนินการอื่นๆ กำลังขโมยอินพุตจากการดำเนินการของคุณตัวอย่างเช่น หากคุณกำลังพยายามผูก WASD คุณอาจเป็นกรณีที่สคริปต์การเคลื่อนไหวตัวละครเริ่มต้นผูกผ่านคีย์เดียวกันเช่นเดียวกับสคริปต์การควบคุมกล้องสามารถขโมยการใส่ขวาคลิกได้หากสคริปต์ทำงานหลังจากสคริปต์ของคุณ

การป้อนข้อมูลโดยไม่ต้องใช้แป้นพิมพ์

บริการนี้มีประโยชน์เป็นพิเศษสำหรับการสนับสนุนเกมแพดและการสัมผัสสำหรับการใส่ข้อมูลจากเกมแพด คุณอาจเลือกที่จะผูกปุ่ม B กับการกระทําที่ส่งผู้ใช้กลับไปที่เมนูก่อนหน้าเมื่อพวกเขาเข้าสู่เมนูอื่นสำหรับการสัมผัส ปุ่มสัมผัสบนหน้าจอสามารถใช้แทนการกดปุ่มได้: ปุ่มเหล่านี้แสดงเฉพาะเมื่อการดำเนินการถูกผูกไว้และตำแหน่ง ข้อความ และ/หรือภาพของปุ่มเหล่านี้สามารถกำหนดค่าผ่านบริการนี้ได้พวกเขามีข้อจํากัดในปริมาณการปรับแต่งที่ให้โดยบริการนี้; มักจะเป็นความคิดที่ดีกว่าที่จะสร้างปุ่มบนหน้าจอของคุณเองโดยใช้ ImageButton หรือ TextButton

ตัวอย่างโค้ด

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
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)

สรุป

วิธีการ

  • BindAction(actionName : string,functionToBind : function,createTouchButton : boolean,inputTypes : Tuple):()

    ผูกการใส่ข้อมูลผู้ใช้กับการดำเนินการที่ให้ฟังก์ชันการจัดการการกระทำ

  • BindActionAtPriority(actionName : string,functionToBind : function,createTouchButton : boolean,priorityLevel : number,inputTypes : Tuple):()

    ทำงานเหมือน BindAction แต่ยังอนุญาตให้มีการกำหนดลําดับความสําคัญสำหรับการดําเนินการที่ผูกไว้สําหรับประเภทอินพุตที่ซ้อนทับ (สูงกว่าต่ํากว่า)

  • BindActivate(userInputTypeForActivation : Enum.UserInputType,keyCodesForActivation : Tuple):()

    ผูก Enum.KeyCode กับ Enum.UserInputType เฉพาะเพื่อเรียกใช้เหตุการณ์ Tool.Activation และ ClickDetector

  • รับตารางข้อมูลเกี่ยวกับการดำเนินการทั้งหมดที่ผูก (คีย์คือชื่อที่ส่งไปยัง BindAction และมูลค่าคือตารางจาก GetBoundActionInfo เมื่อเรียกด้วยคีย์)

  • รับตารางข้อมูลเกี่ยวกับการดำเนินการผูกที่มีชื่อเดิมถูกส่งไปยัง BindAction โดยตรง

  • คืน BackpackItem.TextureId ของ Tool ในปัจจุบัน equipped โดย Player

  • SetDescription(actionName : string,description : string):()

    ให้ชื่อของการดำเนินการผูกด้วยปุ่มสัมผัส แอ็คชัน

  • SetImage(actionName : string,image : string):()

    หากกุญแจ actionName แอ็คชันกุญแจ image จะถูกตั้งเป็นภาพของปุ่มสัมผัส

  • SetPosition(actionName : string,position : UDim2):()

    ในการให้ชื่อของการดำเนินการผูกด้วยปุ่มสัมผัสตั้งตำแหน่งของปุ่มภายใน ContextButtonFrame

  • SetTitle(actionName : string,title : string):()

    ให้ชื่อของการดำเนินการผูกด้วยปุ่มสัมผัส ตั้งข้อความที่แสดงบนปุ่ม

  • UnbindAction(actionName : string):()

    ยกเลิกการผูกการกระทําจากการให้ชื่อให้กับอินพุต

  • UnbindActivate(userInputTypeForActivation : Enum.UserInputType,keyCodeForActivation : Enum.KeyCode):()

    ยกเลิกการผูก Enum.KeyCode กับการกระตุ้น Enum.UserInputType โดยเฉพาะจากการกระตุ้น Tool.Activation เมื่อผูกกับ ContextActionService:BindActivate()

  • ลบฟังก์ชันทั้งหมดที่ผูกไว้ ไม่มีชื่อการกระทําที่จะยังคงอยู่ ปุ่มสัมผัสทั้งหมดจะถูกลบออก

  • GetButton(actionName : string):Instance
    ผลตอบแทน

    ดึงการดำเนินการ ImageButton ของ bound ที่มีปุ่มสัมผัสที่สร้างขึ้น

เหตุการณ์

คุณสมบัติ

วิธีการ

BindAction

()

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""
functionToBind: function
ค่าเริ่มต้น: ""
createTouchButton: boolean
ค่าเริ่มต้น: ""
inputTypes: Tuple
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
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)
General Action Handler

local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Handling action: " .. actionName)
print(inputObj.UserInputType)
end
-- Since this function does not return anything, this handler will
-- "sink" the input and no other action handlers will be called after
-- this one.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
Stacked Action Handlers

local ContextActionService = game:GetService("ContextActionService")
-- Define an action handler for FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler One: " .. actionName)
end
-- This action handler returns nil, so it is assumed that
-- it properly handles the action.
end
-- Binding the action FirstAction (it's on the bottom of the stack)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Define an action handler for SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler Two: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Returning nil implicitly Sinks inputs
return Enum.ContextActionResult.Sink
end
end
-- Binding SecondAction over the first action (since it bound more recently, it is on the top of the stack)
-- Note that SecondAction uses the same keys as
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority

()

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""
functionToBind: function
ค่าเริ่มต้น: ""
createTouchButton: boolean
ค่าเริ่มต้น: ""
priorityLevel: number
ค่าเริ่มต้น: ""
inputTypes: Tuple
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

บริการ ContextAction ผูกการกระทำลำดับความสำคัญ

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(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
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(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
-- โดยไม่ระบุลําดับความสําคัญการดําเนินการที่ผูกไว้ล่าสุดจะถูกเรียกก่อน
-- ดังนั้นการกดปุ่ม INPUT_KEY1 จะพิมพ์ "ต่อย" แล้วจมลงในอินพุต
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- ที่นี่เราผูกทั้งสองฟังก์ชันในลำดับเดียวกับด้านบน แต่มีลําดับความสําคัญที่แลกเปลี่ยนอย่างชัดเจน
-- คือเราให้ความสําคัญกับ "โยน" สูงกว่า 2 ดังนั้นจะถูกเรียกก่อน
-- แม้ว่า "ต่อย" จะยังคงผูกมากขึ้นเมื่อเร็ว ๆ นี้
-- กดปุ่ม INPUT_KEY2 พิมพ์ "โยน" แล้วจมลงในอินพุต
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActivate

()

พารามิเตอร์

userInputTypeForActivation: Enum.UserInputType
ค่าเริ่มต้น: ""
keyCodesForActivation: Tuple
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

GetAllBoundActionInfo


ส่งค่ากลับ

GetBoundActionInfo

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

GetCurrentLocalToolIcon


ส่งค่ากลับ

SetDescription

()

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""
description: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetImage

()

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""
image: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetPosition

()

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""
position: UDim2
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetTitle

()

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""
title: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

UnbindAction

()

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

()

ตัวอย่างโค้ด

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
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

()

พารามิเตอร์

userInputTypeForActivation: Enum.UserInputType
ค่าเริ่มต้น: ""
keyCodeForActivation: Enum.KeyCode
ค่าเริ่มต้น: "Unknown"

ส่งค่ากลับ

()

UnbindAllActions

()

ส่งค่ากลับ

()

GetButton

ผลตอบแทน

พารามิเตอร์

actionName: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

เหตุการณ์

LocalToolEquipped

พารามิเตอร์

toolEquipped: Instance

LocalToolUnequipped

พารามิเตอร์

toolUnequipped: Instance