可綁定的事件和回呼號碼

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

BindableEventBindableFunction 對象讓你能夠在 同一側客戶端-伺服器 邊界之間綁定行為,並傳達經驗內的特定期望結果。

綁定事件最常見的使用案例是對擁有回合結構的體驗。例如,您可能會有一個「比賽開始」事件,其他腳本可以啟動計時器並顯示排行榜,並有相應的「比賽結束」事件,讓其他腳本知道何時將玩家移回大廳並顯示獲勝者。

因為綁定事件可以協調腳本之間的活動,因此通常在服務伺服器上使用,但您也可以在客戶端上使用它們。

根據您的體驗如何運作,可綁定事件可以幫助使您的代碼更具模組化,但 模組腳本 經常是在需要在腳本之間共享數據的情況下的更好選擇。您也可以使用可綁定事件與模組腳本一起,以獲得更清潔的語法,如在 自訂事件 中所述。

可綁定的事件

BindableEvent 對象啟用通過異步、單向通信的腳本之間的自訂事件。

當您通過 方法發射 時,發射腳本不會產生,目標函數會收到傳遞的參數有特定的 限制 。像所有事件一樣,BindableEvents 創建每個連接的功能線程,因此即使發生一個錯誤,其他人也會繼續。

要使用 Studio 中的 BindableEvent 窗口創建新的

  1. 將鼠標懸停在你想要插入 BindableEvent 的容器上。我們建議使用 ServerScriptService 來進行服務器腳本和客戶腳本之間的通信,使用 ReplicatedStorage 來進行客戶腳本之間的通信。
  2. 點擊容器名稱右側的 按鈕,並插入 可綁定事件 個體、實例。
  3. 將實例重命名為 TestBindableEvent

創建了 BindableEvent 後,在一個指令碼本中連接一個功能到其Event事件,然後從另一個指令碼本中Fire()事件。

事件連線

local ServerScriptService = game:GetService("ServerScriptService")
-- 取得可綁定事件實個體、實例的參考
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
-- 連接匿名功能到事件
bindableEvent.Event:Connect(function(data)
print(data) --> 回合開始了!
end)
事件發射

local ServerScriptService = game:GetService("ServerScriptService")
-- 取得可綁定事件實個體、實例的參考
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
-- 火焰綁定事件
bindableEvent:Fire("Round started!")

自訂回呼

BindableFunction 對象允許兩個方向的同步通信,在腳本之間。您可以使用它來定義自訂回呼功能,並手動呼叫 BindableFunction:Invoke() 來啟動它。呼叫函數 的代碼直到找到對應的回調,並且回調接收你傳給 的參數,才會產生 。如果回呼從未設設定,則呼叫它的腳本不會恢復執行。

要使用 Studio 中的 BindableFunction 窗口創建新的

  1. 將鼠標懸停在你想要插入 BindableFunction 的容器上。我們建議使用 ServerScriptService 來進行服務器腳本和客戶腳本之間的通信,使用 ReplicatedStorage 來進行客戶腳本之間的通信。
  2. 點擊容器名稱右側的 按鈕,並插入 可綁定功能 個體、實例。
  3. 將實例重命名為 TestBindableFunction

一旦您創建了 ,您可以在一個指令碼本中連接到其 回調,然後從另一個指令碼本中連接回調功能。

回呼連線

local ServerScriptService = game:GetService("ServerScriptService")
-- 取得可綁定功能的參考
local bindableFunction = ServerScriptService:WaitForChild("TestBindableFunction")
-- 回呼功能
local function addTwoNumbers(a, b)
return a + b
end
-- 將功能設為可綁定的函數的回調
bindableFunction.OnInvoke = addTwoNumbers
事件呼叫

local ServerScriptService = game:GetService("ServerScriptService")
-- 取得可綁定功能的參考
local bindableFunction = ServerScriptService:WaitForChild("TestBindableFunction")
-- 呼叫回調功能並返回輸出值
local sum = bindableFunction:Invoke(2, 4)
print(sum) --> 6

論述限制

當您發射 BindableEvent 或呼叫 BindableFunction 時,它會將您傳給事件或回呼函的任何參數轉發到前方。您可以傳送任何類型的 Roblox 對象 (Enum , Instance ,等等),以及像數字、字串和布林一樣的 Luau 類型,但您應該仔細考慮以下限制。

非字串指數

如果傳送的表中的任何 指數 為非字串類型,例如 Instance用戶資料功能,Roblox 將自動將這些指數轉換為字串。

事件連線

local ServerScriptService = game:GetService("ServerScriptService")
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
local function onEventFire(passedTable)
for k, v in passedTable do
print(typeof(k)) --> 字串
end
end
-- 連接功能到事件
bindableEvent.Event:Connect(onEventFire)
事件發射

local ServerScriptService = game:GetService("ServerScriptService")
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
-- 包含工作區實例的表的火焰鍵件
bindableEvent:Fire({
[workspace.Baseplate] = true
})

表索引化

如果你傳送一個數據表,請勿傳送混合的數字和字串鑰匙表。取而代之,傳送包含 全部 鑰匙值對 (一個辭典) 或 全部 數字指數 (一個陣列) 的表。

事件連線

local ServerScriptService = game:GetService("ServerScriptService")
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
local function onEventFire(passedTable)
for k, v in passedTable do
print(k .. " = " .. v)
--> 1 = 劍
--> 2 = 弓
--> 角色名稱 = 迪瓦龍殺手
--> CharClass = 惡霸
end
end
-- 連接功能到事件
bindableEvent.Event:Connect(onEventFire)
事件發射

local ServerScriptService = game:GetService("ServerScriptService")
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
-- 數字索引表
local inventoryData = {
"Sword", "Bow"
}
-- 字典表
local characterData = {
CharName = "Diva Dragonslayer",
CharClass = "Rogue"
}
-- 使用一致索引的表發出火焰事件
bindableEvent:Fire(inventoryData)
bindableEvent:Fire(characterData)

表身份

傳送到可綁定事件和回呼的表作為參數的表被複製,這意味著它們不會與在發射事件或呼叫回調時提供的一樣準確。被返回給執行人的表也不會與提供的表完全相等。您可以通過在 BindableFunction 上執行以下腳本來展示此差異,然後觀察表身份是如何不同。

回呼連線

local ServerScriptService = game:GetService("ServerScriptService")
local bindableFunction = ServerScriptService:WaitForChild("TestBindableFunction")
-- 回呼功能
local function returnTable(passedTable)
-- 在呼叫時輸出表身份符號化
print(tostring(passedTable)) --> 表:0x48eb7aead27563d9
return passedTable
end
-- 將功能設為可綁定的函數的回調
bindableFunction.OnInvoke = returnTable
事件呼叫

local ServerScriptService = game:GetService("ServerScriptService")
local bindableFunction = ServerScriptService:WaitForChild("TestBindableFunction")
local inventoryData = {
"Sword", "Bow"
}
-- 輸出原始表身份
print(tostring(inventoryData)) --> 表:0x059bcdbb2b576549
local invokeReturn = bindableFunction:Invoke(inventoryData)
-- 返傳回時輸出表身份符號
print(tostring(invokeReturn)) --> table: 0x9fcae7919563a0e9

元表格

如果表有一個可轉換的欄位,所有可轉換的欄位資訊都會在傳輸中丟失。在下面的代碼示例中,NumWheels屬性是Car可匹配的一部分。當伺服器收到下列表時,truck表有Name,但 沒有 屬性NumWheels

事件連線

local ServerScriptService = game:GetService("ServerScriptService")
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
local function onEvent(param)
print(param) --> { ["名稱"] = "MyTruck"]}
end
-- 連接功能到事件
bindableEvent.Event:Connect(onEvent)
事件發射

local ServerScriptService = game:GetService("ServerScriptService")
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
local Car = {}
Car.NumWheels = 4
Car.__index = Car
local truck = {}
truck.Name = "MyTruck"
setmetatable(truck, Car)
-- 包含可轉換表的火焰事件
bindableEvent:Fire(truck)