SharedTable

非推奨を表示

Represents a table-like data structure that can be shared across execution contexts. While it can be used for various sorts of general data storage, it is designed especially for use with Parallel Luau, where it can be used to share state across scripts parented under different Actor instances.

There are a couple idiomatic ways to communicate shared tables between scripts. One method is to store and retrieve SharedTable objects in the SharedTableRegistry. The registry lets any script in the same data model get or set a SharedTable by name. Another method is to use Actor:SendMessage() to send a shared table to another Actor inside a message.

Like a Luau table, a SharedTable object stores a set of key-value element pairs. Unlike a Luau table, only selected types of objects may be stored in a SharedTable, similar to other restrictions you'll find elsewhere in the Roblox engine.

Keys must either be (1) a string or (2) a nonnegative integer number less than 232. Other kinds of keys are not supported.

Values must have one of the following types: Boolean, Number, Vector, String, SharedTable, or a serializable data type. The ability to store a SharedTable as a value in another SharedTable permits the construction of nested and recursive data structures.

SharedTable objects are distinct and different SharedTable objects never compare equal, even if they have contents that would compare equal.

Like a Luau table, a SharedTable object may be frozen, in which case it is read-only. An attempt to modify a frozen SharedTable will raise an error. A frozen SharedTable can be created by first creating a (non-frozen, modifiable) SharedTable with the desired contents, and then calling SharedTable.cloneAndFreeze() to create a frozen clone of it.

コードサンプル

Element Access

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))
Element Iteration

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

new

パラメータ

関数

clear

void

パラメータ

戻り値

void

パラメータ

deep: bool
既定値: false

戻り値

コードサンプル

Shallow Clone

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")
Deep Clone

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

パラメータ

deep: bool
既定値: false

戻り値

increment

パラメータ

戻り値

コードサンプル

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

パラメータ

戻り値

size

パラメータ

戻り値

update

void

パラメータ

戻り値

void

コードサンプル

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")