github_url: | hide |
---|
Inherits: :ref:`Resource<class_Resource>` < :ref:`RefCounted<class_RefCounted>` < :ref:`Object<class_Object>`
Efficiently packs and serializes :ref:`Array<class_Array>` or :ref:`Dictionary<class_Dictionary>`.
.. rst-class:: classref-introduction-group
PackedDataContainer can be used to efficiently store data from untyped containers. The data is packed into raw bytes and can be saved to file. Only :ref:`Array<class_Array>` and :ref:`Dictionary<class_Dictionary>` can be stored this way.
You can retrieve the data by iterating on the container, which will work as if iterating on the packed data itself. If the packed container is a :ref:`Dictionary<class_Dictionary>`, the data can be retrieved by key names (:ref:`String<class_String>`/:ref:`StringName<class_StringName>` only).
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]) # Prints: # key value # lock (0, 0) # another_key 123
Nested containers will be packed recursively. While iterating, they will be returned as :ref:`PackedDataContainerRef<class_PackedDataContainerRef>`.
.. rst-class:: classref-reftable-group
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-method
:ref:`Error<enum_@GlobalScope_Error>` pack(value: :ref:`Variant<class_Variant>`) :ref:`🔗<class_PackedDataContainer_method_pack>`
Packs the given container into a binary representation. The value
must be either :ref:`Array<class_Array>` or :ref:`Dictionary<class_Dictionary>`, any other type will result in invalid data error.
Note: Subsequent calls to this method will overwrite the existing data.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`int<class_int>` size() |const| :ref:`🔗<class_PackedDataContainer_method_size>`
Returns the size of the packed container (see :ref:`Array.size<class_Array_method_size>` and :ref:`Dictionary.size<class_Dictionary_method_size>`).