SharedTable

顯示已棄用項目

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

代表可以在執行上下文之間共享的表狀資料結構。雖然它可以用於各種一般數據存儲,但它專門為與 Parallel Luau 一起使用設計,在那裡它可以用於在不同的 Actor

有一些詞彙方式可以在腳本之間傳送共享表。一種方法是將 SharedTable 物件存儲和取回到 SharedTableRegistry 中。註冊允許任何在相同數據模型中的腳本獲得或設置 SharedTable 名稱。另一種方法是使用 Actor:SendMessage() 將共用表傳送到另一個 Actor 內的消訊息中。

像 Luau 表一樣,SharedTable 個對象儲存一組鑰匙值元素對齊。與 Luau 表不同,只有在共用表中儲存選定類型的對象才能與其他在 Roblox 引擎中找到的限制類似。

鑰匙必須是(1)字串或(2)負數小於232的非負整數。其他類型的鑰匙不支持。

值必須具有以下類型之一:Boolean、Number、Vector、String、SharedTable 或可序列化的數據輸入。能夠將 SharedTable 存儲為另一個 SharedTable 的值,允許建立孤立和鏈接的資料結構。

SharedTable 對象是不同的,不同的 SharedTable 對象永遠不會比較等值,即使它們有相同的內容。

像 Luau 表一樣,SharedTable 對象可能被凍結,在此情況下它是閱讀僅限的。嘗試修改凍結的 SharedTable 將會發生錯誤。凍結的 SharedTable 可以先創建一個(非凍結、可修改) SharedTable 包含所需內容,然後呼叫 SharedTable.cloneAndFreeze() 創建一個凍結複製。

範例程式碼

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.

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

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.

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

返回新的、空的 SharedTable


local st = SharedTable.new()

new

返回一個新的 SharedTable 包含與提供的 Luau 表中的元素相等的元素。

如果提供的 Luau 表包含任何無法儲存在 SharedTable 中的鑰匙或值,則無法建立 SharedTable 。查看此頁面頂部的總結,以獲得一個列表存儲在 SharedTable 中的對象類型。如果 Luau 表包含任何表作為值,該表將轉換為新的 SharedTable


local t = {}
t.x = 1
t.y = 2
t.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

()

原子地移除所有元素的 SharedTable

如果 SharedTable 被凍結,操作將失敗,並且會發生錯誤。


local st = SharedTable.new({"a", "b", "c"})
assert(SharedTable.size(st) == 3)
SharedTable.clear(st)
assert(SharedTable.size(st) == 0)

參數

要清除的 SharedTable

返回

()

創建一個 SharedTable 的複製並返回複製。

如果選擇的 deep 參數不存在,或者如果存在且其值為 false ,則會創建淺複製。淺複製只複製最高級別的 SharedTable 對物件。如果 SharedTable 本身的任何值是 SharedTable ,那麼原始的 SharedTable 和複製的 SharedTable 都會指向相同的 SharedTable

淺複製操作是原子的,因此複製 SharedTable 將包含原始 SharedTable 狀態的一致截圖,即使在同時從其他腳本修改時。

如果可選擇的 deep 參數存在且其值為 true,則會創建深複製。深複製會徹底複製 SharedTable 個對象的結構,使原本的 SharedTable 和複製之間沒有狀態共享。

圖中每個 對象的複製是原子的,但整體深複製不是原子的。因此,圖中每個 SharedTable 的複製將包含與原始 SharedTable 對象相同的狀態截圖,但不同的 SharedTable 對象狀態可能不一致,如果圖正在同時從其他腳本修改時。

被複製的 SharedTable 物件象可能是凍結的(只能讀取)或不是。無論如何,新創建的複製本是被凍結(因此可修改)。要創建冰凍複製人,請使用 SharedTable.cloneAndFreeze 功能。

為了說明淺複製和深複製之間的差異,請考慮以下樣本。第一個樣本創建了淺複製,第二個創建了深複製。

參數

要複製的 SharedTable 對象。

deep: boolean

是否創建深複製 ( true ) 或淺複製 ( false )。

預設值:false

範例程式碼

This code sample creates a shallow clone of a SharedTable.

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

This code sample creates a deep clone of a SharedTable.

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

創建一個凍結(僅讀)複製的 SharedTable 並返回複製。這個函數的行為與複製的行為相同,除了複製被凍結。

如果要求深複製,那麼所有複製的 SharedTable 對象都會被凍結。

參數

要複製的共用表對象。

deep: boolean

是否創建深複製 ( true ) 或淺複製 ( false )。

預設值:false

increment

原子增加元素的值。具有指定鍵的元素必須存在於 SharedTable 中,且必須為 number 類型。指定的 delta 被添加到值,原始值被返回。

SharedTable.update 功能也可用於此目的;此 increment 功能存在於方便和性能(一般來說,incrementupdate 快得多,因此在可能的情況下應該優先使用)。以下兩個功能呼叫具有相同效果:


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)

如果 SharedTable 被凍結,操作將失敗,並且會發生錯誤。

參數

要更新的 SharedTable 對象。

key: string | number

要更新的 SharedTable 對象中元素的鑰匙。

delta: number

SharedTable 中要添加到元素的值。

返回

元素的原始值,在 delta 被添加到它之前。

範例程式碼

This code sample demonstrates usage of SharedTable.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

如果 SharedTable 被凍結 (只讀),返回 true


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] = nil
assert(SharedTable.size(st) == 2)

參數

要查詢的 SharedTable 對象的尺寸。

返回

update

()

原子更新元素的值。

當在不同執行上下文中運行的腳本同時存取 SharedTable 時,它們的存取可能會無法預測地交叉。因此,以下代碼通常不正確,因為值可能在第一行閱讀和第二行更新之間發生變化:


local oldValue = st["x"]
st["x"] = oldValue .. ",x"

更新功能讓您能夠對元素進行原子更新。它需要一個函數,它將使用元素目前的值來呼叫。函數可以計算並返回新值。請注意,如果 SharedTable 被從其他腳本同時修改,那麼該功能可能會被多次呼叫。

如果 SharedTable 被凍結,操作將失敗,並且會發生錯誤。

參數

要更新的 SharedTable 對象。

key: string | number

要更新的 SharedTable 對象中元素的鑰匙。

會被呼叫以計算元素的新值的功能。

返回

()

範例程式碼

This code sample demonstrates usage of SharedTable.update().

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