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.
Checking the stable version of the documentation...
PackedDataContainer
继承: 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。
方法
size() const |
方法说明
将给定的容器打包为二进制表示。value
必须为 Array 或 Dictionary,其他类型会导致无效数据错误。
注意:后续再次调用该方法会覆盖已有数据。
返回打包后容器的大小(见 Array.size() 和 Dictionary.size())。