SharedTable
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
実行コンテキスト間で共有できるテーブルのようなデータ構造を表します。並列 Luau の場合、Parallel Luau を使用して、スクリプトの親子間で状態を共有できます。
スクリプト間の共有テーブルを通信するためのいくつかの idiomatic な方法があります。1つのメソッドは、SharedTable 内の SharedTableRegistry オブジェクトをストアし、取り出すことです
Luau テーブルのように、 SharedTable オブジェクトはキー値の要素ペアを保存します。Luau テーブルとは、Roblox エンジンの他の制限と同様、共有テーブルにのみ選択されたオブジェクトが保存できます。
キーは (1) 文字列であるか、(2) 2 32 以下の非正数の整数である必要があります。他の種類のキーはサポートされていません。
値は次のタイプの 1つを持つ必要があります:ブースト、数値、ベクトル、文字入力、 SharedTable 、またはシリアル化可能なデータータイプ。SharedTable を値として保存することで、ネストされたおよび再帰的なデーター構造を構築できます。
SharedTable オブジェクトは、異なる SharedTable オブジェクトは、同じ内容を持つことがあっても、異なるように比較されません。
Luau テーブルのように、 SharedTable オブジェクトは凍結されることがあり、その場合は読み取りのみが可能になります。フローズンの Datatype.SharedTable</
コードサンプル
Elements in a SharedTable are accessed in the same way as elements of Luau tables, using the st[k] syntax or, for string keys, st.k.
local st = SharedTable.new()
st[1] = "a"
st["x"] = true
st.y = 5
assert(st[1] == "a")
assert(st["x"] == true)
assert(st.x == true)
assert(st["y"] == 5)
assert(st.y == 5)
-- true is not a valid SharedTable key, so attempting to set that key
-- fails:
assert(not pcall(function() st[true] = 100 end))
-- A function is not a valid SharedTable value, so attempting to set a
-- value to a function fails:
assert(not pcall(function() st["f"] = function() end end))
The for loop can be used to iterate over the elements of a SharedTable. The elements of the SharedTable are not iterated directly. Instead, a shallow clone is made of the SharedTable, and its elements are iterated. This is done to ensure that a consistent view of the SharedTable is maintained throughout the iteration. Thus, both of the following for loops have the same behavior.
Note that this means that if the SharedTable is accessed directly from within the body of the iteration loop, its state may not be consistent with the state observed through the iteration.
The iteration order is partially specified. Elements with numeric keys are iterated before elements with string keys. Elements with numeric keys are iterated in ascending numeric order. Elements with string keys are iterated in an unspecified order.
local st = SharedTable.new({"a", "b", "c"})
for k, v in SharedTable.clone(st, false) do
print(k, ": ", v)
end
for k, v in st do
print(k, ": ", v)
end
概要
コンストラクタ
- new()
新しい、空の SharedTable を返します。
提供された Luau テーブルに同等の要素を含む新しい SharedTable を返します。
関数
Datatype.SharedTable からすべての要素を削除します。
提供された SharedTable のクローンを作成し、返します。
提供された SharedTable のクローンを作成し、読み取りのみ返します。
delta をプロバイドキーと共有し、元の値を返します。
Datatype.SharedTable がフローズンされた場合、SharedTable を返します。
Datatype.SharedTable に保存されたエレメントの数を返します。
提供された更新機能を介して、値を提供します。
コンストラクタ
new
提供された Luau テーブルに同等の要素を含む新しい SharedTable を返します。
提供された Luau テーブルには、<a href="/reference/engine/datatypes"> Datatype.SharedTable に保存できない値またはキーが含まれている場合、<a href="/reference/engine/datatypes"> Datatype.SharedTable の構築に失敗します。このページのトップにあるサマ
local t = {}t.x = 1t.y = 2t.z = {"a", "b", "c"}local st = SharedTable.new(t)assert(st.x == 1)assert(st.y == 2)assert(st.z[1] == "a")assert(st.z[2] == "b")assert(st.z[3] == "c")
注意、一部の場合、SharedTable を SharedTableRegistry に保存することが便利です。Class.ShareTableRegistry:GetSharedTable() メソッドは、これを実現する便利な方法を提供します。
パラメータ
新しい SharedTable に保存する要素のある Luau テーブル。
関数
clear
Atomically removes all of the elements from a SharedTable .
Datatype.SharedTable が凍結されている場合、オペレーションに失敗し、エラーが発生します。
local st = SharedTable.new({"a", "b", "c"})assert(SharedTable.size(st) == 3)SharedTable.clear(st)assert(SharedTable.size(st) == 0)
パラメータ
クリアするた消すの SharedTable。
戻り値
clone
クローンする SharedTable を作成し、クローンを返します。
オプションの deep 引数が存在しないか、またはその値が false である場合、 Shallow ク
深くないクローン操作はアトミックなので、クローン SharedTable には、他のスクリプトから現在変更されている場合でも、状態のコンシステントなスナップショットが含まれています。
オプションの deep 引数が存在し、その値が true である場合、深いクローンが作成されます。深いクローンは、オリジナルの SharedTable オブジェクトの構造をコピーします。これにより、クローンとオリジナルの 1>Datatype.SharedTable1> の間に状態共有
グラフ内の各 SharedTable オブジェクトのクローンはアトミックですが、クローン全体はアトミックではありません。つまり、グラフ内の各 Datatype.SharedTable
クローンされる SharedTable オブジェクト(読み取りのみ)または、 Datatype.SharedTable )を凍結(読み取りのみ)する可能性があります。どちらにせよ、新しく作成されたクローンは 不要なクローン ではありません。To create frozen clones, use the function 1>Datatype.SharedTable.cloneAndFreeze</
深くないクローンと浅いクローンの違いを示すには、次のサンプルを考慮してください。この最初のサンプルは浅いクローンを作成し、この 2つ目のサンプルは深いクローンを作成します。
パラメータ
クローンする SharedTable オブジェクト。
深いクローンを作成するか浅いクローンを作成するか。
戻り値
コードサンプル
This code sample creates a shallow clone of a SharedTable.
local original = SharedTable.new()
original["a"] = "original a"
original["b"] = "original b"
original["c"] = SharedTable.new()
original["c"]["d"] = "original d"
local clone = SharedTable.clone(original, false)
clone["a"] = "new a"
clone["b"] = "new b"
clone["c"]["d"] = "new d"
assert(original["a"] == "original a")
assert(original["b"] == "original b")
-- Because this was a shallow clone, original["c"] and clone["c"] are
-- the same SharedTable object.
assert(original["c"] == clone["c"])
assert(original["c"]["d"] == "new d")
This code sample creates a deep clone of a SharedTable.
local original = SharedTable.new()
original["a"] = "original a"
original["b"] = "original b"
original["c"] = SharedTable.new()
original["c"]["d"] = "original d"
local clone = SharedTable.clone(original, true)
clone["a"] = "new a"
clone["b"] = "new b"
clone["c"]["d"] = "new d"
assert(original["a"] == "original a")
assert(original["b"] == "original b")
-- Because this was a deep clone, clone["c"] is a clone of original["c"];
-- they are distinct SharedTable objects.
assert(original["c"] ~= clone["c"])
assert(original["c"]["d"] == "original d")
cloneAndFreeze
Datatype.SharedTable のクローンを冷凍し、読み取りのみクローンを返します。この関数の動作は、クローンと同じですが、クローンが冷凍されていることです。
深いクローンが要求される場合は、クローンされた SharedTable すべてのオブジェクトが凍結されます。
パラメータ
共有テーブルオブジェクトをクローンする。
深いクローンを作成するか浅いクローンを作成するか。
戻り値
increment
要素の値を原子的に増加させます。指定されたキーの要素は SharedTable に存在する必要があり、タイプは number でなければなりません。指定された delta は、値に追加され、元の値が返されます。
この目的でも SharedTable.update 機能を使用できます。この increment 機能は、便利性とパフォーマンスのために存在します (一全般的に、increment は 2> アップデートpdate2> よりも速い) のため、可能である場合は 5>increment5> を使用することをお勧めしま
local st = SharedTable.new()
st["x"] = 1
local oldValue = SharedTable.increment(st, "x", 1)
SharedTable.update(st, "x", function(v)
oldValue = v
return v + 1
end)
Datatype.SharedTable が凍結されている場合、オペレーションに失敗し、エラーが発生します。
パラメータ
戻り値
Delta が追加される前の要素のオリジナルの価値。
コードサンプル
This code sample demonstrates usage of SharedTable.increment().
local st = SharedTable.new()
st["x"] = 1
local oldValue = SharedTable.increment(st, "x", 1)
assert(oldValue == 1)
assert(st["x"] == 2)
-- The value of the specified key must be a number. If it is not a
-- number, the call will fail:
st["y"] = "test"
assert(not pcall(function() SharedTable.increment(st, "y", 1) end))
isFrozen
Datatype.SharedTable がフローズンされた場合、SharedTable を返します。
local st1 = SharedTable.new({"a", "b", "c"})assert(not SharedTable.isFrozen(st1))local st2 = SharedTable.cloneAndFreeze(st1)assert(SharedTable.isFrozen(st2))
パラメータ
凍結状態の SharedTable オブジェクト。
戻り値
size
共有テーブルに保存された要素の数を返します。この他のスクリプトが共有テーブルを変更している場合、返されたサイズは、返された後に正しくなる可能性がありますが、他のスクリプトが共有テーブルから要素を追加または削除した可能性があるため、返されたサイズは正しくなる可能性がありません。
local st = SharedTable.new({"a", "b", "c"})assert(SharedTable.size(st) == 3)st[2] = nilassert(SharedTable.size(st) == 2)
パラメータ
サイズを指定する必要がある SharedTable オブジェクト。
戻り値
update
要素の値を原子的に更新します。
Datatype.SharedTable が異なる実行コンテキストのスクリプトから同時にアクセスされると、アクセスが予測不能に交差する可能性があります。これにより、コードの次のようなが一般的に間違っています。コードは通常、最初の行の読み取りと 2 番目の行の更新の間で変更される可能性があります。
local oldValue = st["x"]st["x"] = oldValue .. ",x"
アップデート機能は、要素にアトミックアップデートを実行することを可能にします。要素の現在の値を呼び出す関数を取ります。その後、関数は新しい値を計算して返すことができます。注意、関数は SharedTable が他のスクリプトから同時に変更されている場合、複数回呼び出される可能性があります。
Datatype.SharedTable が凍結されている場合、オペレーションに失敗し、エラーが発生します。
パラメータ
戻り値
コードサンプル
This code sample demonstrates usage of SharedTable.update().
local st = SharedTable.new()
st["x"] = "abcd"
SharedTable.update(st, "x", function(v)
assert(v == "abcd")
return v .. "e"
end)
assert(st["x"] == "abcde")