バインド可能なイベントとコールバック

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

および オブジェクトでは、クライアント-サーバー境界の 同じ側 のスクリプト間で動作をバインドし、経験中のアクションに対する特定の期待結果を伝達できます。

バインド可能なイベントの最も一般的な使用ケースは、ラウンドベースの構造を持つエクスペリエンスです。たとえば、他のスクリプトがタイマーを開始し、リーダーボースコアボードを表示する「マッチ開始」イベントと、他のスクリプトがプレイヤーをロビーに戻すタイミングと勝者を表示する「マッチ終了」イベントがある場合、「マッチ開始」イベントに対応する「マッチ終了」イベントがあります。

スクリプト間の活動を調整するため、バインド可能イベントは通常、サーバーで使用されますが、クライアントでも使用できます。

エクスペリエンスの動作に応じて、バインド可能なイベントはコードをよりモジュラルにするのに役立つ可能性がありますが、モジュールスクリプトは、スクリプト間でデータを共有する必要がある状況でよく代替として使用されます。また、カスタムイベント に記載されたように、モジュールスクリプトとバインド可能イベントを組み合わせて、より洗練された構文を使用できます。

バインド可能なイベント

BindableEvent オブジェクトは、スクリプト間の非同期、一方通行の通信を介してカスタムイベントを有効にします。

メソッドを通じて を発射すると、発射スクリプトは しません 、ターゲット関数は、特定の 制限 でパスされた引数を受け取ります。すべてのイベントと同様、BindableEvents 各接続された機能のスレッドを作成し、たとえ一つのエラーが発生しても、他の人が続行します。

Studio の BindableEvent ウィンドウを使用して新しい を作成するには:

  1. 挿入したい BindableEvent をコンテナにホバーします。サーバースクリプトとクライアントスクリプトの間の通信には ServerScriptService を使用し、クライアントスクリプトとサーバースクリプトの間の通信には ReplicatedStorage を使用することをお勧めします。
  2. コンテナの名前の右側に表示される ボタンをクリックし、 バインド可能イベント インスタンスを挿入します。
  3. インスタンスを TestBindableEvent に名前変更します。

After you've created a BindableEvent , connect a function to its Event event in one script, and then Fire() the event from another script.

イベント接続

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 に名前変更します。

一度、BindableFunction を作成したら、1つのスクリプトでその OnInvoke コールバックに接続し、別のスクリプトからコールバック関数を Invoke() できます。

呼び出しコネクション

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

引数の制限

When you fire a BindableEvent or invoke a BindableFunction , it forwards any arguments that you pass with the event or to the callback function.任意の Roblox オブジェクト (EnumInstance、など) や Luau タイプ (数字、文字列、ブールなど) のようなものを通過できますが、次の制限を注意深く考慮する必要があります。

非ストリングインデックス

パスされたテーブルのどの インデックス も、Instanceuserdata 、または 機能 のような非ストリングタイプの場合、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 = ボウ
--> CharName = ディヴァドラゴンスレイヤー
--> 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)
-- 呼び出し時の出力テーブルID 識別
print(tostring(passedTable)) --> テーブル: 0x48eb7aead27563d9
return passedTable
end
-- 機能をバインド可能な関数のコールバックとして設定
bindableFunction.OnInvoke = returnTable
イベントの招待

local ServerScriptService = game:GetService("ServerScriptService")
local bindableFunction = ServerScriptService:WaitForChild("TestBindableFunction")
local inventoryData = {
"Sword", "Bow"
}
-- 出力オリジナルテーブルID
print(tostring(inventoryData)) --> テーブル: 0x059bcdbb2b576549
local invokeReturn = bindableFunction:Invoke(inventoryData)
-- 返却時の出力テーブルID 識別
print(tostring(invokeReturn)) --> table: 0x9fcae7919563a0e9

メタテーブル

テーブルにメタテーブルがある場合、すべてのメタテーブル情報が転送中に失われます。次のコードサンプルでは、NumWheels プロパティは Car メタテーブルの一部です。サーバーが次の表を受信すると、 テーブルには プロパティがありますが、 プロパティはありません。

イベント接続

local ServerScriptService = game:GetService("ServerScriptService")
local bindableEvent = ServerScriptService:WaitForChild("TestBindableEvent")
local function onEvent(param)
print(param) --> {["Name"] = "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)