Lớp công cụ
ContextActionService
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Phương Pháp
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():() |
Sự Kiện
LocalToolEquipped(toolEquipped: Instance):RBXScriptSignal |
LocalToolUnequipped(toolUnequipped: Instance):RBXScriptSignal |
Mẫu mã
Công cụ Reload dịch vụ 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("Đang nạp lại!")
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)Tài Liệu Tham Khảo API
Phương Pháp
BindAction
Lợi Nhuận
()
Mẫu mã
Công cụ Reload dịch vụ 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("Đang nạp lại!")
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)Bộ Xử Lý Hành Động Chung
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Đang xử lý hành động: " .. actionName)
print(inputObj.UserInputType)
end
-- Vì hàm này không trả về gì cả, bộ xử lý này sẽ
-- "chìm" đầu vào và không có bộ xử lý hành động nào khác sẽ được gọi sau
-- bộ xử lý này.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)Các Trình Xử Lý Hành Động Chồng Chéo
local ContextActionService = game:GetService("ContextActionService")
-- Định nghĩa một trình xử lý hành động cho FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Trình Xử Lý Hành Động Một: " .. actionName)
end
-- Trình xử lý hành động này trả về nil, vì vậy nó được giả định là
-- nó xử lý hành động một cách chính xác.
end
-- Liên kết hành động FirstAction (nó ở dưới cùng của ngăn xếp)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Định nghĩa một trình xử lý hành động cho SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Trình Xử Lý Hành Động Hai: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Trả về nil ngầm định làm chìm đầu vào
return Enum.ContextActionResult.Sink
end
end
-- Liên kết SecondAction lên trên hành động đầu tiên (vì nó được liên kết gần đây hơn, nó ở trên cùng của ngăn xếp)
-- Lưu ý rằng SecondAction sử dụng cùng các phím như
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)BindActionAtPriority
Lợi Nhuận
()
Mẫu mã
Ưu tiên BindAction của 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(`Hành động [{actionName}] đã xảy ra. KeyCode [{inputObject.KeyCode}] đã được nhấn.`)
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(`Hành động [{actionName}] đã xảy ra. KeyCode [{inputObject.KeyCode}] đã được nhấn.`)
return Enum.ContextActionResult.Sink
end
-- Nếu không chỉ định ưu tiên, hành động được liên kết gần đây nhất sẽ được gọi trước,
-- vì vậy nhấn INPUT_KEY1 sẽ in ra "Punch" và sau đó hủy bỏ đầu vào.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Ở đây chúng ta liên kết cả hai hàm theo cùng thứ tự như trên, nhưng với các ưu tiên đã hoán đổi rõ ràng.
-- Tức là, chúng ta cho "Throw" một mức ưu tiên cao hơn là 2 để nó sẽ được gọi trước,
-- mặc dù "Punch" vẫn được liên kết gần đây hơn.
-- Nhấn INPUT_KEY2 sẽ in ra "Throw" và sau đó hủy bỏ đầu vào.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)BindActionToInputTypes
BindActivate
ContextActionService:BindActivate(
):()
Tham Số
Lợi Nhuận
()
GetBoundActionInfo
Tham Số
Lợi Nhuận
GetButton
SetDescription
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng thoại với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng các hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một cái gì đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp với nút bằng cách sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
endSetImage
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng thoại với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng các hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một cái gì đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp với nút bằng cách sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
endSetPosition
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng thoại với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng các hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một cái gì đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp với nút bằng cách sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
endSetTitle
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng thoại với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng các hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một cái gì đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp với nút bằng cách sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
endUnbindAction
Tham Số
Lợi Nhuận
()
Mẫu mã
Công cụ Reload dịch vụ 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("Đang nạp lại!")
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(
):()
Tham Số
| Giá Trị Mặc Định: "None" |
Lợi Nhuận
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
Lợi Nhuận
()
Sự Kiện
LocalToolEquipped
Tham Số
LocalToolUnequipped
Tham Số