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ề bất cứ điều gì, 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)Bộ xử lý hành động xếp chồng
local ContextActionService = game:GetService("ContextActionService")
-- Định nghĩa một bộ xử lý hành động cho **FirstAction**
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Bộ xử lý hành động một: " .. actionName)
end
-- Bộ 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ó nằm ở 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 bộ xử lý hành động cho **SecondAction**
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Bộ 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 Sinking inputs
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ó nằm ở trên cùng của ngăn xếp)
-- Lưu ý rằng **SecondAction** sử dụng cùng một phím
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
-- Không chỉ định ưu tiên, hành động mới nhất được gắn sẽ được gọi trước,
-- vì vậy nhấn INPUT_KEY1 sẽ in "Đấm" và sau đó sink đầu vào.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Ở đây chúng ta gắn cả hai hàm theo cùng thứ tự như trên, nhưng với các ưu tiên được hoán đổi rõ ràng.
-- Nghĩa là, chúng ta cho "Ném" một ưu tiên cao hơn là 2 để nó sẽ được gọi trước,
-- mặc dù "Đấm" vẫn được gắn gần đây hơn.
-- Nhấn INPUT_KEY2 sẽ in "Ném" và sau đó sink đầ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 = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nó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 những 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 thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút 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 = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nó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 những 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 thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút 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 = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nó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 những 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 thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút 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 = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nó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 những 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 thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút 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: "Unknown" |
Lợi Nhuận
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
Lợi Nhuận
()
Sự Kiện
LocalToolEquipped
Tham Số
LocalToolUnequipped
Tham Số