variant_class.rst 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. .. _doc_variant_class:
  2. Variant class
  3. =============
  4. About
  5. -----
  6. Variant is the most important datatype of Godot, it's the most important
  7. class in the engine. A Variant takes up only 20 bytes and can store
  8. almost any engine datatype inside of it. Variants are rarely used to
  9. hold information for long periods of time, instead they are used mainly
  10. for communication, editing, serialization and generally moving data
  11. around.
  12. A Variant can:
  13. - Store almost any datatype
  14. - Perform operations between many variants (GDScript uses Variant as
  15. its atomic/native datatype).
  16. - Be hashed, so it can be compared quickly to over variants
  17. - Be used to convert safely between datatypes
  18. - Be used to abstract calling methods and their arguments (Godot
  19. exports all its functions through variants)
  20. - Be used to defer calls or move data between threads.
  21. - Be serialized as binary and stored to disk, or transferred via
  22. network.
  23. - Be serialized to text and use it for printing values and editable
  24. settings.
  25. - Work as an exported property, so the editor can edit it universally.
  26. - Be used for dictionaries, arrays, parsers, etc.
  27. Basically, thanks to the Variant class, writing Godot itself was a much,
  28. much easier task, as it allows for highly dynamic constructs not common
  29. of C++ with little effort. Become a friend of Variant today.
  30. References:
  31. ~~~~~~~~~~~
  32. - `core/variant.h <https://github.com/godotengine/godot/blob/master/core/variant.h>`__
  33. Dictionary and Array
  34. --------------------
  35. Both are implemented using variants. A Dictionary can match any datatype
  36. used as key to any other datatype. An Array just holds an array of
  37. Variants. Of course, a Variant can also hold a Dictionary and an Array
  38. inside, making it even more flexible.
  39. Both have a shared mode and a COW mode. Scripts often use them in shared
  40. mode (meaning modifications to a container will modify all references to
  41. it), or COW mode (modifications will always alter the local copy, making
  42. a copy of the internal data if necessary, but will not affect the other
  43. copies). In COW mode, Both Dictionary and Array are thread-safe,
  44. otherwise a Mutex should be created to lock if multi thread access is
  45. desired.
  46. References:
  47. ~~~~~~~~~~~
  48. - `core/dictionary.h <https://github.com/godotengine/godot/blob/master/core/dictionary.h>`__
  49. - `core/array.h <https://github.com/godotengine/godot/blob/master/core/array.h>`__