Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

PackedDataContainer

已棄用: Use @GlobalScope.var_to_bytes() or FileAccess.store_var() instead. To enable data compression, use PackedByteArray.compress() or FileAccess.open_compressed().

繼承: Resource < RefCounted < Object

將 Array 或 Dictionary 進行高效打包和序列化。

說明

PackedDataContainer 可以高效地儲存未指定型別的容器中的資料。資料會以原始位元組的形式打包,能夠儲存到檔案中。只有 Array 和 Dictionary 能夠這樣儲存。

你可以通過遍歷容器來取得資料,效果和遍歷被打包的資料一樣。如果打包容器為 Dictionary,則取得的是鍵名(僅 String/StringName)。

var data = { "key": "value", "another_key": 123, "lock": Vector2() }
var packed = PackedDataContainer.new()
packed.pack(data)
ResourceSaver.save(packed, "packed_data.res")
var container = load("packed_data.res")
for key in container:
    prints(key, container[key])

輸出:

key value
lock (0, 0)
another_key 123

內嵌容器會遞歸打包。遍歷時回傳的是 PackedDataContainerRef。

方法

Error

pack(value: Variant)

int

size() const


方法說明

Error pack(value: Variant) 🔗

將給定的容器打包為二進位表示。value 必須為 Array 或 Dictionary,其他型別會導致無效資料錯誤。

注意:後續再次呼叫該方法會覆蓋已有資料。


int size() const 🔗

返回打包後容器的大小(見 Array.size() 和 Dictionary.size())。