class_packeddatacontainer.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/PackedDataContainer.xml.
  6. .. _class_PackedDataContainer:
  7. PackedDataContainer
  8. ===================
  9. **Deprecated:** Use :ref:`@GlobalScope.var_to_bytes()<class_@GlobalScope_method_var_to_bytes>` or :ref:`FileAccess.store_var()<class_FileAccess_method_store_var>` instead. To enable data compression, use :ref:`PackedByteArray.compress()<class_PackedByteArray_method_compress>` or :ref:`FileAccess.open_compressed()<class_FileAccess_method_open_compressed>`.
  10. **Inherits:** :ref:`Resource<class_Resource>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  11. Efficiently packs and serializes :ref:`Array<class_Array>` or :ref:`Dictionary<class_Dictionary>`.
  12. .. rst-class:: classref-introduction-group
  13. Description
  14. -----------
  15. **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.
  16. 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).
  17. ::
  18. var data = { "key": "value", "another_key": 123, "lock": Vector2() }
  19. var packed = PackedDataContainer.new()
  20. packed.pack(data)
  21. ResourceSaver.save(packed, "packed_data.res")
  22. ::
  23. var container = load("packed_data.res")
  24. for key in container:
  25. prints(key, container[key])
  26. Prints:
  27. .. code:: text
  28. key value
  29. lock (0, 0)
  30. another_key 123
  31. Nested containers will be packed recursively. While iterating, they will be returned as :ref:`PackedDataContainerRef<class_PackedDataContainerRef>`.
  32. .. rst-class:: classref-reftable-group
  33. Methods
  34. -------
  35. .. table::
  36. :widths: auto
  37. +---------------------------------------+-------------------------------------------------------------------------------------------------+
  38. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`pack<class_PackedDataContainer_method_pack>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
  39. +---------------------------------------+-------------------------------------------------------------------------------------------------+
  40. | :ref:`int<class_int>` | :ref:`size<class_PackedDataContainer_method_size>`\ (\ ) |const| |
  41. +---------------------------------------+-------------------------------------------------------------------------------------------------+
  42. .. rst-class:: classref-section-separator
  43. ----
  44. .. rst-class:: classref-descriptions-group
  45. Method Descriptions
  46. -------------------
  47. .. _class_PackedDataContainer_method_pack:
  48. .. rst-class:: classref-method
  49. :ref:`Error<enum_@GlobalScope_Error>` **pack**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_PackedDataContainer_method_pack>`
  50. 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.
  51. \ **Note:** Subsequent calls to this method will overwrite the existing data.
  52. .. rst-class:: classref-item-separator
  53. ----
  54. .. _class_PackedDataContainer_method_size:
  55. .. rst-class:: classref-method
  56. :ref:`int<class_int>` **size**\ (\ ) |const| :ref:`🔗<class_PackedDataContainer_method_size>`
  57. Returns the size of the packed container (see :ref:`Array.size()<class_Array_method_size>` and :ref:`Dictionary.size()<class_Dictionary_method_size>`).
  58. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  59. .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
  60. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  61. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  62. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  63. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  64. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  65. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  66. .. |void| replace:: :abbr:`void (No return value.)`