Engine Class
ContextActionService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
วิธีการ
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():() |
เหตุการณ์
LocalToolEquipped(toolEquipped: Instance):RBXScriptSignal |
LocalToolUnequipped(toolUnequipped: Instance):RBXScriptSignal |
ตัวอย่างโค้ด
เครื่องมือ ContextActionService รีโหลด
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("กำลังรีโหลด!")
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
วิธีการ
BindAction
ส่งค่ากลับ
()
ตัวอย่างโค้ด
เครื่องมือ ContextActionService รีโหลด
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("กำลังรีโหลด!")
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)ตัวจัดการการกระทำทั่วไป
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("กำลังจัดการการกระทำ: " .. actionName)
print(inputObj.UserInputType)
end
-- เนื่องจากฟังก์ชันนี้ไม่คืนค่าหมายความว่า ตัวจัดการนี้จะ
-- "จม" การป้อนข้อมูลและตัวจัดการการกระทำอื่น ๆ จะไม่ถูกเรียกหลัง
-- ตัวนี้.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)ตัวจัดการการกระทำซ้อนกัน
local ContextActionService = game:GetService("ContextActionService")
-- กำหนดฟังก์ชันจัดการสำหรับ FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("ฟังก์ชันจัดการการกระทำหนึ่ง: " .. actionName)
end
-- ฟังก์ชันจัดการการกระทำนี้คืนค่า nil ดังนั้นจึงถือว่ามัน
-- จัดการการกระทำอย่างถูกต้อง.
end
-- เชื่อมโยงการกระทำ FirstAction (มันอยู่ที่ด้านล่างของสแต็ก)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- กำหนดฟังก์ชันจัดการสำหรับ SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("ฟังก์ชันจัดการการกระทำสอง: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- การคืนค่า nil จะทำให้ Sink inputs
return Enum.ContextActionResult.Sink
end
end
-- เชื่อมโยง SecondAction เหนือการกระทำแรก (เนื่องจากมันถูกเชื่อมโยงเมื่อไม่นานมานี้ มันจึงอยู่ที่ด้านบนของสแต็ก)
-- หมายเหตุว่าการกระทำที่สองใช้ปุ่มเดียวกันกับ
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)BindActionAtPriority
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ลำดับความสำคัญของการผูกการกระทำของ 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(`การกระทำ [{actionName}] เกิดขึ้น รหัสคีย์ [{inputObject.KeyCode}] ถูกกด.`)
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(`การกระทำ [{actionName}] เกิดขึ้น รหัสคีย์ [{inputObject.KeyCode}] ถูกกด.`)
return Enum.ContextActionResult.Sink
end
-- โดยไม่มีการกำหนดความสำคัญ การกระทำที่ถูกผูกล่าสุดจะถูกเรียกก่อน,
-- ดังนั้นการกด INPUT_KEY1 จะพิมพ์ "Punch" และจากนั้นจะทำการซื่งข้อมูล.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- ที่นี่เราผูกทั้งสองฟังก์ชันในลำดับเดียวกันกับด้านบน แต่โดยมีการสลับความสำคัญอย่างชัดเจน.
-- กล่าวคือ เรามอบความสำคัญที่สูงกว่า 2 ให้กับ "Throw" ดังนั้นมันจะถูกเรียกก่อน,
-- แม้ว่า "Punch" จะถูกผูกเมื่อเร็ว ๆ นี้.
-- การกด INPUT_KEY2 จะพิมพ์ "Throw" และจากนั้นจะทำการซื่งข้อมูล.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)BindActionToInputTypes
BindActivate
ContextActionService:BindActivate(
):()
พารามิเตอร์
ส่งค่ากลับ
()
GetBoundActionInfo
พารามิเตอร์
ส่งค่ากลับ
GetButton
SetDescription
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
endSetImage
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
endSetPosition
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
endSetTitle
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
endUnbindAction
พารามิเตอร์
ส่งค่ากลับ
()
ตัวอย่างโค้ด
เครื่องมือ ContextActionService รีโหลด
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("กำลังรีโหลด!")
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(
):()
พารามิเตอร์
| ค่าเริ่มต้น: "Unknown" |
ส่งค่ากลับ
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
ส่งค่ากลับ
()
เหตุการณ์
LocalToolEquipped
พารามิเตอร์
LocalToolUnequipped
พารามิเตอร์