variant_class.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. .. _doc_variant_class:
  2. Variant class
  3. =============
  4. About
  5. -----
  6. Variant is the most important datatype in Godot. A Variant takes up only 24
  7. bytes on 64-bit platforms (20 bytes on 32-bit platforms) and can store almost
  8. any engine datatype inside of it. Variants are rarely used to hold information
  9. for long periods of time, instead they are used mainly for communication,
  10. editing, serialization and generally moving data around.
  11. A Variant can:
  12. - Store almost any datatype.
  13. - Perform operations between many variants (GDScript uses Variant as
  14. its atomic/native datatype).
  15. - Be hashed, so it can be compared quickly to other variants.
  16. - Be used to convert safely between datatypes.
  17. - Be used to abstract calling methods and their arguments (Godot
  18. exports all its functions through variants).
  19. - Be used to defer calls or move data between threads.
  20. - Be serialized as binary and stored to disk, or transferred via
  21. network.
  22. - Be serialized to text and use it for printing values and editable
  23. settings.
  24. - Work as an exported property, so the editor can edit it universally.
  25. - Be used for dictionaries, arrays, parsers, etc.
  26. Basically, thanks to the Variant class, writing Godot itself was a much,
  27. much easier task, as it allows for highly dynamic constructs not common
  28. of C++ with little effort. Become a friend of Variant today.
  29. .. note::
  30. All types within Variant except Nil and Object **cannot** be ``null`` and
  31. must always store a valid value. These types within Variant are therefore
  32. called *non-nullable* types.
  33. One of the Variant types is *Nil* which can only store the value ``null``.
  34. Therefore, it is possible for a Variant to contain the value ``null``, even
  35. though all Variant types excluding Nil and Object are non-nullable.
  36. References
  37. ~~~~~~~~~~
  38. - `core/variant/variant.h <https://github.com/godotengine/godot/blob/master/core/variant/variant.h>`__
  39. List of variant types
  40. ---------------------
  41. These types are available in Variant:
  42. +---------------------------------+---------------------------+
  43. | Type | Notes |
  44. +=================================+===========================+
  45. | Nil (can only store ``null``) | Nullable type |
  46. +---------------------------------+---------------------------+
  47. | :ref:`class_bool` | |
  48. +---------------------------------+---------------------------+
  49. | :ref:`class_int` | |
  50. +---------------------------------+---------------------------+
  51. | :ref:`class_float` | |
  52. +---------------------------------+---------------------------+
  53. | :ref:`class_string` | |
  54. +---------------------------------+---------------------------+
  55. | :ref:`class_vector2` | |
  56. +---------------------------------+---------------------------+
  57. | :ref:`class_vector2i` | |
  58. +---------------------------------+---------------------------+
  59. | :ref:`class_rect2` | 2D counterpart of AABB |
  60. +---------------------------------+---------------------------+
  61. | :ref:`class_rect2i` | |
  62. +---------------------------------+---------------------------+
  63. | :ref:`class_vector3` | |
  64. +---------------------------------+---------------------------+
  65. | :ref:`class_vector3i` | |
  66. +---------------------------------+---------------------------+
  67. | :ref:`class_transform2d` | |
  68. +---------------------------------+---------------------------+
  69. | :ref:`class_vector4` | |
  70. +---------------------------------+---------------------------+
  71. | :ref:`class_vector4i` | |
  72. +---------------------------------+---------------------------+
  73. | :ref:`class_plane` | |
  74. +---------------------------------+---------------------------+
  75. | :ref:`class_quaternion` | |
  76. +---------------------------------+---------------------------+
  77. | :ref:`class_aabb` | 3D counterpart of Rect2 |
  78. +---------------------------------+---------------------------+
  79. | :ref:`class_basis` | |
  80. +---------------------------------+---------------------------+
  81. | :ref:`class_transform3d` | |
  82. +---------------------------------+---------------------------+
  83. | :ref:`class_projection` | |
  84. +---------------------------------+---------------------------+
  85. | :ref:`class_color` | |
  86. +---------------------------------+---------------------------+
  87. | :ref:`class_stringname` | |
  88. +---------------------------------+---------------------------+
  89. | :ref:`class_nodepath` | |
  90. +---------------------------------+---------------------------+
  91. | :ref:`class_rid` | |
  92. +---------------------------------+---------------------------+
  93. | :ref:`class_object` | Nullable type |
  94. +---------------------------------+---------------------------+
  95. | :ref:`class_callable` | |
  96. +---------------------------------+---------------------------+
  97. | :ref:`class_signal` | |
  98. +---------------------------------+---------------------------+
  99. | :ref:`class_dictionary` | |
  100. +---------------------------------+---------------------------+
  101. | :ref:`class_array` | |
  102. +---------------------------------+---------------------------+
  103. | :ref:`class_packedbytearray` | |
  104. +---------------------------------+---------------------------+
  105. | :ref:`class_packedint32array` | |
  106. +---------------------------------+---------------------------+
  107. | :ref:`class_packedint64array` | |
  108. +---------------------------------+---------------------------+
  109. | :ref:`class_packedfloat32array` | |
  110. +---------------------------------+---------------------------+
  111. | :ref:`class_packedfloat64array` | |
  112. +---------------------------------+---------------------------+
  113. | :ref:`class_packedstringarray` | |
  114. +---------------------------------+---------------------------+
  115. | :ref:`class_packedvector2array` | |
  116. +---------------------------------+---------------------------+
  117. | :ref:`class_packedvector3array` | |
  118. +---------------------------------+---------------------------+
  119. | :ref:`class_packedcolorarray` | |
  120. +---------------------------------+---------------------------+
  121. | :ref:`class_packedvector4array` | |
  122. +---------------------------------+---------------------------+
  123. Containers: Array and Dictionary
  124. --------------------------------
  125. Both :ref:`class_array` and :ref:`class_dictionary` are implemented using
  126. variants. A Dictionary can match any datatype used as key to any other datatype.
  127. An Array just holds an array of Variants. Of course, a Variant can also hold a
  128. Dictionary or an Array inside, making it even more flexible.
  129. Modifications to a container will modify all references to
  130. it. A Mutex should be created to lock it if
  131. :ref:`multi-threaded access <doc_using_multiple_threads>` is desired.
  132. References
  133. ~~~~~~~~~~
  134. - `core/variant/dictionary.h <https://github.com/godotengine/godot/blob/master/core/variant/dictionary.h>`__
  135. - `core/variant/array.h <https://github.com/godotengine/godot/blob/master/core/variant/array.h>`__