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.
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):()
ผูกการใส่ข้อมูลผู้ใช้กับการดำเนินการที่ให้ฟังก์ชันการจัดการการกระทำ
ทำงานเหมือน BindAction แต่ยังอนุญาตให้มีการกำหนดลําดับความสําคัญสำหรับการดําเนินการที่ผูกไว้สําหรับประเภทอินพุตที่ซ้อนทับ (สูงกว่าต่ํากว่า)
ผูก Enum.KeyCode กับ Enum.UserInputType เฉพาะเพื่อเรียกใช้เหตุการณ์ Tool.Activation และ ClickDetector
รับตารางข้อมูลเกี่ยวกับการดำเนินการทั้งหมดที่ผูก (คีย์คือชื่อที่ส่งไปยัง BindAction และมูลค่าคือตารางจาก GetBoundActionInfo เมื่อเรียกด้วยคีย์)
รับตารางข้อมูลเกี่ยวกับการดำเนินการผูกที่มีชื่อเดิมถูกส่งไปยัง BindAction โดยตรง
คืน BackpackItem.TextureId ของ Tool ในปัจจุบัน equipped โดย Player
ให้ชื่อของการดำเนินการผูกด้วยปุ่มสัมผัส แอ็คชัน
หากกุญแจ actionName แอ็คชันกุญแจ image จะถูกตั้งเป็นภาพของปุ่มสัมผัส
ในการให้ชื่อของการดำเนินการผูกด้วยปุ่มสัมผัสตั้งตำแหน่งของปุ่มภายใน ContextButtonFrame
ให้ชื่อของการดำเนินการผูกด้วยปุ่มสัมผัส ตั้งข้อความที่แสดงบนปุ่ม
ยกเลิกการผูกการกระทําจากการให้ชื่อให้กับอินพุต
- UnbindActivate(userInputTypeForActivation : Enum.UserInputType,keyCodeForActivation : Enum.KeyCode):()
ยกเลิกการผูก Enum.KeyCode กับการกระตุ้น Enum.UserInputType โดยเฉพาะจากการกระตุ้น Tool.Activation เมื่อผูกกับ ContextActionService:BindActivate()
ลบฟังก์ชันทั้งหมดที่ผูกไว้ ไม่มีชื่อการกระทําที่จะยังคงอยู่ ปุ่มสัมผัสทั้งหมดจะถูกลบออก
ดึงการดำเนินการ ImageButton ของ bound ที่มีปุ่มสัมผัสที่สร้างขึ้น
อีเวนต์
คุณสมบัติ
วิธีการ
BindAction
ผูกการดำเนินการกับการใส่ของผู้ใช้ที่มีฟังก์ชันการจัดการการดำเนินการเมื่อมีการใส่ข้อมูลที่ตรงกันทำแล้ว ฟังก์ชันตัวรับแอคชันจะถูกเรียกด้วยอาร์กิวเมนต์ที่ระบุด้านล่างรายการเอนเนมอินพุตที่ถูกต้องรวมถึงรายการภายในสิ่งต่อไปนี้: Enum.KeyCode , Enum.UserInputType หรือ Enum.PlayerActions .เรียกฟังก์ชันนี้เมื่อผู้เล่น เข้าสู่บริบท ซึ่งสามารถดำเนินการได้เมื่อผู้เล่นออกจากบริบท โทร UnbindAction ด้วย actionName เดียวกันคุณสามารถเรียกฟังก์ชันการจัดการการดำเนินการของการดำเนินการโดยใช้ CallFunction ได้ด้วยตนเอง
ตัวอย่างโค้ดด้านล่างแสดงวิธีการที่ Sound สามารถเป็น played ในขณะที่กุญแจ ( H ) ปุ่มแผ่นเกม หรือปุ่มหน้าจอสัมผัสถูกกด
local ContextActionService = game:GetService("ContextActionService")
-- เสียงแตรรถ
local honkSound = Instance.new("Sound", workspace)
honkSound.Looped = true
honkSound.SoundId = "rbxassetid://9120386436"
local function handleAction(actionName, inputState, inputObject)
if actionName == "HonkHorn" then
if inputState == Enum.UserInputState.Begin then
honkSound:Play()
else
honkSound:Pause()
end
end
end
-- เมื่อผู้เล่นนั่งอยู่ในรถ:
ContextActionService:BindAction("HonkHorn", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
-- เมื่อผู้เล่นออก:
ContextActionService:UnbindAction("HonkHorn")
พารามิเตอร์ตัวจัดการการดำเนินการ
ฟังก์ชันตัวจัดการการดำเนินการเรียกด้วยพารามิเตอร์ต่อไปนี้:
<th>ประเภท</th><th>คําอธิบาย</th></tr><tr><td>1</td><td><code>สตริง</code></td><td>สตริงเดียวกันที่ถูกส่งไปยัง BindAction† เดิม</td></tr><tr><td>2</td><td><code>Enum.UserInputState</code></td><td>สถานะของอินพุต (เริ่ม เปลี่ยน สิ้นสุด หรือยกเลิก)*</td></tr><tr><td>3</td><td><code>วัตถุอินพุต</code></td><td>วัตถุที่มีข้อมูลเกี่ยวกับการใส่ (แตกต่างกันขึ้นอยู่กับ UserInputType)</td></tr>
# |
† นี้ช่วยให้ฟังก์ชันหนึ่งสามารถจัดการการดำเนินการหลายอย่างได้ในครั้งเดียว หากจำเป็นการยกเลิกจะถูกส่งหากมีการป้อนข้อมูลบางอย่างอยู่ในระหว่างดำเนินการและการกระทำอื่นถูกผูกกับข้อมูลที่ป้อนอยู่ในระหว่างดำเนินการหรือหากการกระทำที่ผูกอยู่ในระหว่างดำเนินการมีค่าเป็น unbound
กองการผูกแอคชัน
การผูกการกระทําจะทํางานเหมือนกับกอง: หากการกระทําสองอย่างถูกผูกกับการใส่ของผู้ใช้เดียวกัน ตัวจัดการการดําเนินการล่าสุดที่ผูกไว้จะถูกใช้หากตัวจัดการการดำเนินการส่งคืน Enum.ContextActionResult.Pass ตัวจัดการการดำเนินการที่ผูกไว้ล่าสุดจะถูกเรียกต่อไปจนกว่าจะมีการส่งอินพุต (โดยการกลับ nil หรือ Enum.ContextActionResult.Sink )เมื่อ UnbindAction ถูกเรียก ตัวจัดการการดำเนินการจะถูกลบออกจากสแต็กพฤติกรรมการซ้อนกันนี้สามารถถูกแทนที่โดยใช้ BindActionAtPriority ซึ่งพารามิเตอร์ลําดับความสําคัญเพิ่มเติมหลังจาก createTouchButton อาจยกเลิกลําดับความสําคัญของการดําเนินการ (สูงกว่าต่ํากว่า)
ปุ่มสัมผัส
นอกจากประเภทอินพุตแล้ว พารามิเตอร์ที่สามของฟังก์ชันนี้ควบคุมว่าจะสร้างปุ่มสําหรับอุปกรณ์ TouchEnabled การสร้างสรรค์ปุ่มชื่อ ScreenGui ที่เรียกว่า "ContextActionGui" จะถูกเพิ่มลงใน PlayerGuiภายใน ScreenGui มี Frame ที่เรียกว่า "ContextButtonFrame" เพิ่มขึ้นมันอยู่ในกรอบนี้ซึ่ง ImageButtons สำหรับการดำเนินการผูกจะถูกผูก; คุณสามารถใช้ GetButton เพื่อดึงปุ่มดังกล่าวสำหรับการปรับแต่งได้
พารามิเตอร์
สตริงที่แทนที่การดำเนินการที่ดำเนินการ (เช่น "HonkHorn" หรือ "OpenDoor")
ฟังก์ชันการจัดการการดำเนินการเรียกด้วยพารามิเตอร์ต่อไปนี้เมื่ออินพุตที่ผูกถูกกระตุ้น: string (actionName), Enum.UserInputState และ InputObject
ว่าควรสร้างปุ่ม GUI สำหรับการดำเนินการบนอุปกรณ์สัมผัสหรือไม่
จํานวนใดก็ได้ของ Enum.KeyCode หรือ Enum.UserInputType ที่แทนที่การใส่ข้อมูลเพื่อผูกกับการดําเนินการ
ส่งค่ากลับ
ตัวอย่างโค้ด
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.
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)
This code sample uses ContextActionService to bind an action named "BoundAction" to a general action handler function on the F key. Place this in a LocalScript inside StarterPlayerScripts and press F to see the message "Handling action: BoundAction".
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)
This code sample demonstrates how BindAction acts like a stack. It binds two actions, FirstAction (Z, X, and C keys) and SecondAction (Z and X keys) to two action handling functions. The second one will pass on a certain input (the X key).
Both actions use the Z and X keys, however the second handler will pass input only if X is pressed. So, when X is pressed, the second handler is called and then the first. The first action is also bound to the C key, and can be triggered even though the other two inputs are "covered" by the second action.
Test this code out by pasting it into a LocalScript within StarterPlayerScripts, then pressing Z, X and C. Observe which action handlers are called with what actions.
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
BindActionAtPriority ทำงานเหมือน BindAction แต่ยังอนุญาตให้มีการกำหนดลําดับความสําคัญให้กับการดำเนินการที่ผูกไว้หากการดำเนินการหลายอย่างมีการผูกกับอินพุตเดียวกัน ฟังก์ชันลําดับความสําคัญสูงสุดจะถูกเรียกไม่ว่าคำสั่งจะถูกผูกตามลําดับใดคือการทำซ้ำฟังก์ชันนี้ทำลายพฤติกรรม "กอง" ปกติของ BindAction
พารามิเตอร์
สตริงที่แทนที่การดำเนินการที่ดำเนินการ (เช่น "HonkHorn" หรือ "OpenDoor")
ฟังก์ชันการจัดการการดำเนินการเรียกด้วยพารามิเตอร์ต่อไปนี้เมื่ออินพุตที่ผูกถูกกระตุ้น: string (actionName), Enum.UserInputState และ InputObject
ว่าควรสร้างปุ่ม GUI สำหรับการดำเนินการบนอุปกรณ์สัมผัสหรือไม่
ระดับความสําคัญที่การดําเนินการควรจะผูกติดอยู่ (สูงกว่าที่พิจารณาก่อนต่ํากว่า)
จํานวนใดก็ได้ของ Enum.KeyCode หรือ Enum.UserInputType ที่แทนที่การใส่ข้อมูลเพื่อผูกกับการดําเนินการ
ส่งค่ากลับ
ตัวอย่างโค้ด
ตัวอย่างโค้ดนี้แสดงวิธีที่ ContextActionService:BindActionAtPriority() สามารถใช้เพื่อผูกการดำเนินการออกจากลำดับแต่ยังคงมีระดับความสำคัญเดียวกันปกติ BindAction() จะทำงานตามลำดับ (การดำเนินการล่าสุดมีลําดับความสําคัญสูงสุด) แต่ระดับความสําคัญจะยกเลิกการดําเนินการนี้คุณสามารถทดสอบโค้ดนี้โดยวางลงใน Script ด้วย RunContext = Client ใน ReplicatedStorage
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
ผูก Enum.KeyCode ที่สามารถใช้กับ Enum.UserInputType เพื่อเปิดใช้งานกิจกรรม ClickDetector และ Tools และ GuiButtonsเมื่อกุญแจ/ปุ่มที่กำหนดถูกกด มันจะส่งเหตุการณ์ Mouse.Button1Down บนเมาส์ที่ส่งไปยัง Tool.Equippedสิ่งนี้จะยิงอีเวนต์ Tool.Activated หาก Tool.ManualActivationOnly ไม่ได้ถูกตั้งค่าเป็นจริงสำหรับการใส่เกมแพด, ฟังก์ชันนี้จะถูกเรียกโดยสคริปต์การควบคุมเริ่มต้นเพื่อผูกปุ่ม ButtonR2 Enum.KeyCode
โปรดทราบว่า Enum.UserInputType ที่ระบุจะต้องเป็น Keyboard หรือ Gamepad1 ผ่าน Gamepad8 เพื่อให้ถูกต้อง
พารามิเตอร์
ต้องเป็นแป้นพิมพ์หรือเกมแพด1ผ่านเกมแพด8
ส่งค่ากลับ
GetAllBoundActionInfo
GetAllBoundActioninfo คือการคืนตารางที่แปลชื่อการกระทําทั้งหมด (ซึ่งเดิมถูกส่งไปยัง BindAction ) ไปยังตารางที่คืนโดย GetBoundActionInfo เมื่อเรียกด้วยชื่อการกระทําเองโดยใช้ฟังก์ชันนี้คุณสามารถตรวจสอบการดำเนินการทั้งหมดที่ผูกอยู่ในปัจจุบันได้มีประโยชน์เมื่อแก้ไขระดับลําดับความสําคัญหรือคําสั่งซื้อชั้นวางของพวกเขา
ส่งค่ากลับ
GetBoundActionInfo
GetBoundActionInfo คือการคืนตารางที่มีกุญแจต่อไปนี้อธิบายถึงการดำเนินการผูกที่ได้รับชื่อเพื่อรับข้อมูลเดียวกันสำหรับการดำเนินการทั้งหมดในครั้งเดียว ให้ใช้ GetAllBoundActionInfo
<th>ประเภท</th><th>คําอธิบาย</th></tr><tr><td><code>stackOrder</code></td><td>จํานวน</td><td>อธิบายดัชนีของการดำเนินการบนสแต็ก (เพิ่มขึ้น)</td></tr><tr><td><code>ระดับความสําคัญ</code> \*</td><td>จํานวน</td><td>อธิบายระดับ <code>Class.ContextActionService:BindActionAtPriority()|priority</code> แอ็คชัน</td></tr><tr><td><code>สร้างปุ่มสัมผัส</code></td><td>bool</td><td>อธิบายว่าควรสร้างปุ่มแตะบนอุปกรณ์ <code>Class.UserInputService.TouchEnabled|TouchEnabled</code> หรือไม่</td></tr><tr><td><code>ประเภทการใส่</code></td><td>ตาราง</td><td>ประเภทอินพุทที่ส่งไปยัง <code>Class.ContextActionService:BindAction()|BindAction</code> ซึ่งการดำเนินการนี้จะเรียกใช้</td></tr><tr><td><code>คําอธิบาย</code> †</td><td>สตริง</td><td>คำอธิบายของการกระทําที่กําหนดโดย <code>Class.ContextActionService:SetDescription()|SetDescription</code></td></tr><tr><td><code>ชื่อ</code> †</td><td>สตริง</td><td>ชื่อของการกระทําที่กําหนดโดย <code>Class.ContextActionService:SetTitle()|SetTitle</code></td></tr><tr><td><code>รูปภาพ</code> †</td><td>ข้อความ</td><td>แอ็คชัน<code>Class.ContextActionService:SetImage()|SetImage</code></td></tr>
ชื่อ |
ระดับความสําคัญจะยังคงรวมอยู่แม้ว่า BindActionAtPriority จะไม่ถูกใช้ - โดยค่าเริ่มต้นจะเป็น 2000
† บ่งบอกว่าฟิลด์นี้จะเป็น nil หากวิธีที่เกี่ยวข้องไม่ถูกเรียกสําหรับการดําเนินการที่กำหนด
พารามิเตอร์
ส่งค่ากลับ
GetCurrentLocalToolIcon
GetCurrentLocalToolIcon จะคืน BackpackItem.TextureId ของ Tool ในขณะนี้ equipped โดย Player หรือ nil หากไม่มีเครื่องมือดังกล่าวหรือหากผู้เล่นขาด Character
ส่งค่ากลับ
สตริงเนื้อหาจาก TextureId ของเครื่องมือหรือ nil หากไม่พบได้
SetDescription
การตั้งค่าคำอธิบายจะตั้งค่าคำอธิบายของการดำเนินการที่ผูกโดย แอ็คชัน
แม้ว่าชื่ออาจบ่งบอกว่าวิธีนี้เกี่ยวข้องกับครอบครัวของฟังก์ชันที่ปรับแต่งปุ่มสัมผัสสำหรับการดำเนินการที่สร้างพวกเขา ( SetTitle , SetImage และ SetPosition ), วิธีนี้ไม่ส่งผลต่อปุ่มดังกล่าววิธีนี้เพียงตั้งค่าคำอธิบายข้อความของการดำเนินการและไม่มีอะไรมากไปกว่านี้
พารามิเตอร์
ชื่อของการดำเนินการที่เดิมถูกส่งไปยัง BindAction
คําอธิบายข้อความของการดําเนินการ เช่น "แตรรถฮอนก" หรือ "คลังไอเท็ม"
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
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
วิธีนี้ตั้งภาพที่แสดงบนปุ่มสัมผัสที่สร้างโดย BindAction()โดยเฉพาะอย่างยิ่งมันกำหนดคุณสมบัติ ImageLabel.Image ของ ImageLabel ภายใน ImageButton ที่จะถูกส่งคืนโดย GetButtonหากไม่มีการดำเนินการผูกแบบนั้น (เช่นไม่มีอะไรถูกส่งคืนโดย GetButton) ฟังก์ชันนี้ไม่ทำอะไรและไม่โยนข้อผิดพลาด
ฟังก์ชันนี้เป็นส่วนหนึ่งของครอบครัวของวิธีการที่ปรับแต่งปุ่มสัมผัสของการดำเนินการ คนอื่นๆ ในครอบครัวนี้รวมถึง SetPosition และ SetTitle
พารามิเตอร์
ชื่อของการดำเนินการที่เดิมถูกส่งไปยัง BindAction
ค่าที่ควรตั้งค่าคุณสมบัติรูปภาพ
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
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
วิธีนี้ตั้งตำแหน่งของปุ่มสัมผัสที่สร้างโดย BindAction()โดยเฉพาะอย่างยิ่งมันกำหนดคุณสมบัติ GuiObject.Position ของ ImageButton ที่จะถูกส่งคืนโดย GetButtonหากไม่มีการดำเนินการผูกแบบนั้น (เช่นไม่มีอะไรถูกส่งคืนโดย GetButton) ฟังก์ชันนี้ไม่ทำอะไรและไม่โยนข้อผิดพลาด
ฟังก์ชันนี้เป็นส่วนหนึ่งของครอบครัวของวิธีการที่ปรับแต่งปุ่มสัมผัสของการดำเนินการ คนอื่นๆ ในครอบครัวนี้รวมถึง SetImage และ SetTitle
พารามิเตอร์
ชื่อของการดำเนินการที่เดิมถูกส่งไปยัง BindAction
ตำแหน่งภายใน ContextButtonFrame
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
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
ตั้งชื่อจะตั้งข้อความที่แสดงบนปุ่มสัมผัสที่สร้างโดย BindActionโดยเฉพาะ, สิ่งนี้ตั้งค่าคุณสมบัติ TextLabel.Text ของ TextLabel ภายใน ImageButton ที่จะถูกส่งคืนโดย GetButtonหากไม่มีการดำเนินการผูกแบบนั้น (เช่นไม่มีอะไรถูกส่งคืนโดย GetButton) ฟังก์ชันนี้ไม่ทำอะไรและไม่โยนข้อผิดพลาด
ฟังก์ชันนี้เป็นส่วนหนึ่งของครอบครัวของวิธีการที่ปรับแต่งปุ่มสัมผัสของการดำเนินการ คนอื่นๆ ในครอบครัวนี้รวมถึง SetImage และ SetPosition
พารามิเตอร์
ชื่อของการดำเนินการที่เดิมถูกส่งไปยัง BindAction
ข้อความที่จะแสดงบนปุ่ม
ส่งค่ากลับ
ตัวอย่างโค้ด
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
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
การดำเนินการยกเลิกจะยกเลิกการดำเนินการโดยใช้ชื่อจากการใส่ของผู้ใช้เพื่อให้ฟังก์ชันตัวจัดการการดำเนินการไม่ถูกเรียกอีกต่อไปเรียกฟังก์ชันนี้เมื่อบริบทสำหรับการดำเนินการบางอย่างไม่สามารถใช้งานได้อีกต่อไป เช่น ปิดอินเทอร์เฟซผู้ใช้ ออกจากรถ หรือ unequipping ออกจาก Toolดู BindAction สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการทำงานของการดำเนินการผูก
ฟังก์ชันนี้ จะไม่โยนข้อผิดพลาดถ้าไม่มีการดำเนินการดังกล่าวผูกกับสตริงที่กำหนด โดยใช้ GetAllBoundActionInfo หรือแท็บ "การผูกแอคชัน" ของคอนโซลนักพัฒนาคุณสามารถค้นหาว่าแอคชันใดที่ผูกอยู่ในปัจจุบันได้
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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.
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
ยกเลิกการเปิดใช้งานจะเปิดใช้งานการผูก Enum.KeyCode ที่ใช้กับ Enum.UserInputType เพื่อเปิดใช้งาน Tool (หรือ HopperBin ) โดยใช้ BindActivate .ฟังก์ชันนี้ทำลายการดำเนินการที่ดำเนินการโดยฟังก์ชันนั้นเป็นหลัก
พารามิเตอร์
ประเภทการใส่ข้อมูลผู้ใช้เดียวกันที่ส่งไปยัง BindActivate เดิม
รหัสเดียวกันที่ส่งไปยัง BindActivate เดิม
ส่งค่ากลับ
UnbindAllActions
ลบฟังก์ชันทั้งหมดที่ผูกไว้ไม่มีชื่อการกระทําที่จะยังคงอยู่ปุ่มสัมผัสทั้งหมดจะถูกลบหากปุ่มถูกควบคุมด้วยตนเองไม่มีการรับประกันว่าจะถูกล้างออก
ส่งค่ากลับ
GetButton
ได้รับปุ่มส่งคืน ImageButton ที่สร้างโดย BindAction หากพารามิเตอร์ที่สามเป็นจริงและอุปกรณ์เป็น TouchEnabledพารามิเตอร์เดียวสำหรับฟังก์ชันนี้ต้องตรงกับชื่อของการดำเนินการที่ส่งไปยัง BindAction เดิมอย่างถูกต้อง
หากไม่มีการดำเนินการดังกล่าวถูกผูกหรือหากปุ่มไม่ถูกสร้าง ฟังก์ชันนี้จะคืน nil
พารามิเตอร์
ชื่อของการดำเนินการที่เดิมถูกส่งไปยัง BindAction
ส่งค่ากลับ
ปุ่มภาพที่สร้างโดย BindAction