c_sharp_variant.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. .. _doc_c_sharp_variant:
  2. C# Variant
  3. ==========
  4. For a detailed explanation of Variant in general, see the :ref:`Variant <class_Variant>` documentation page.
  5. ``Godot.Variant`` is used to represent Godot's native :ref:`Variant <class_Variant>` type. Any
  6. :ref:`Variant-compatible type <c_sharp_variant_compatible_types>` can be converted from/to it.
  7. We recommend avoiding ``Godot.Variant`` unless it is necessary to interact with untyped engine APIs.
  8. Take advantage of C#'s type safety when possible.
  9. Any of ``Variant.As{TYPE}`` methods or the generic ``Variant.As<T>`` method can be used to convert
  10. a ``Godot.Variant`` to a C# type. Since the ``Godot.Variant`` type contains implicit conversions
  11. defined for all the supported types, calling these methods directly is usually not necessary.
  12. Use ``CreateFrom`` method overloads or the generic ``Variant.From<T>`` method to convert a C# type
  13. to a ``Godot.Variant``.
  14. .. note::
  15. Since the Variant type in C# is a struct, it can't be null. To create a "null"
  16. Variant use the ``default`` keyword or the parameterless constructor.
  17. .. _c_sharp_variant_compatible_types:
  18. Variant-compatible types
  19. ------------------------
  20. A Variant-compatible type can be converted to and from a ``Godot.Variant``.
  21. These C# types are Variant-compatible:
  22. * All the `built-in value types <https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/built-in-types-table>`_,
  23. except ``decimal``, ``nint`` and ``nuint``.
  24. * ``string``.
  25. * Classes derived from :ref:`GodotObject <class_Object>`.
  26. * Collections types defined in the ``Godot.Collections`` namespace.
  27. Full list of Variant types and their equivalent C# type:
  28. ======================= ===========================================================
  29. Variant.Type C# Type
  30. ======================= ===========================================================
  31. ``Nil`` ``null`` (Not a type)
  32. ``Bool`` ``bool``
  33. ``Int`` ``long`` (Godot stores 64-bit integers in Variant)
  34. ``Float`` ``double`` (Godot stores 64-bit floats in Variant)
  35. ``String`` ``string``
  36. ``Vector2`` ``Godot.Vector2``
  37. ``Vector2I`` ``Godot.Vector2I``
  38. ``Rect2`` ``Godot.Rect2``
  39. ``Rect2I`` ``Godot.Rect2I``
  40. ``Vector3`` ``Godot.Vector3``
  41. ``Vector3I`` ``Godot.Vector3I``
  42. ``Transform2D`` ``Godot.Transform2D``
  43. ``Vector4`` ``Godot.Vector4``
  44. ``Vector4I`` ``Godot.Vector4I``
  45. ``Plane`` ``Godot.Plane``
  46. ``Quaternion`` ``Godot.Quaternion``
  47. ``Aabb`` ``Godot.Aabb``
  48. ``Basis`` ``Godot.Basis``
  49. ``Transform3D`` ``Godot.Transform3D``
  50. ``Projection`` ``Godot.Projection``
  51. ``Color`` ``Godot.Color``
  52. ``StringName`` ``Godot.StringName``
  53. ``NodePath`` ``Godot.NodePath``
  54. ``Rid`` ``Godot.Rid``
  55. ``Object`` ``Godot.GodotObject`` or any derived type.
  56. ``Callable`` ``Godot.Callable``
  57. ``Signal`` ``Godot.Signal``
  58. ``Dictionary`` ``Godot.Collections.Dictionary``
  59. ``Array`` ``Godot.Collections.Array``
  60. ``PackedByteArray`` ``byte[]``
  61. ``PackedInt32Array`` ``int[]``
  62. ``PackedInt64Array`` ``long[]``
  63. ``PackedFloat32Array`` ``float[]``
  64. ``PackedFloat64Array`` ``double[]``
  65. ``PackedStringArray`` ``string[]``
  66. ``PackedVector2Array`` ``Godot.Vector2[]``
  67. ``PackedVector3Array`` ``Godot.Vector3[]``
  68. ``PackedColorArray`` ``Godot.Color[]``
  69. ======================= ===========================================================
  70. .. warning::
  71. Godot uses 64-bit integers and floats in Variant. Smaller integer and float types
  72. such as ``int``, ``short`` and ``float`` are supported since they can fit in the
  73. bigger type. Be aware that an implicit conversion is performed so using the wrong
  74. type will result in potential precision loss.
  75. .. warning::
  76. Enums are supported by ``Godot.Variant`` since their underlying type is an integer
  77. type which are all compatible. However, implicit conversions don't exist, enums must
  78. be manually converted to their underlying integer type before they can converted to/from
  79. ``Godot.Variant`` or use the generic ``Variant.As<T>`` and ``Variant.From<T>`` methods
  80. to convert them.
  81. .. code-block:: csharp
  82. enum MyEnum { A, B, C }
  83. Variant variant1 = (int)MyEnum.A;
  84. MyEnum enum1 = (MyEnum)(int)variant1;
  85. Variant variant2 = Variant.From(MyEnum.A);
  86. MyEnum enum2 = variant2.As<MyEnum>();
  87. Using Variant in a generic context
  88. ----------------------------------
  89. When using generics, you may be interested in restricting the generic ``T`` type to be
  90. only one of the Variant-compatible types. This can be achieved using the ``[MustBeVariant]``
  91. attribute.
  92. .. code-block:: csharp
  93. public void MethodThatOnlySupportsVariants<[MustBeVariant] T>(T onlyVariant)
  94. {
  95. // Do something with the Variant-compatible value.
  96. }
  97. Combined with the generic ``Variant.From<T>`` allows you to obtain an instance of ``Godot.Variant``
  98. from an instance of a generic ``T`` type. Then it can be used in any API that only supports the
  99. ``Godot.Variant`` struct.
  100. .. code-block:: csharp
  101. public void Method1<[MustBeVariant] T>(T variantCompatible)
  102. {
  103. Variant variant = Variant.From(variantCompatible);
  104. Method2(variant);
  105. }
  106. public void Method2(Variant variant)
  107. {
  108. // Do something with variant.
  109. }
  110. In order to invoke a method with a generic parameter annotated with the ``[MustBeVariant]``
  111. attribute, the value must be a Variant-compatible type or a generic ``T`` type annotated
  112. with the ``[MustBeVariant]`` attribute as well.
  113. .. code-block:: csharp
  114. public class ObjectDerivedClass : GodotObject { }
  115. public class NonObjectDerivedClass { }
  116. public void Main<[MustBeVariant] T1, T2>(T1 someGeneric1, T2 someGeneric2)
  117. {
  118. MyMethod(42); // Works because `int` is a Variant-compatible type.
  119. MyMethod(new ObjectDerivedClass()); // Works because any type that derives from `GodotObject` is a Variant-compatible type.
  120. MyMethod(new NonObjectDerivedClass()); // Does NOT work because the type is not Variant-compatible.
  121. MyMethod(someGeneric1); // Works because `T1` is annotated with the `[MustBeVariant]` attribute.
  122. MyMethod(someGeneric2); // Does NOT work because `T2` is NOT annotated with the `[MustBeVariant]` attribute.
  123. }
  124. public void MyMethod<[MustBeVariant] T>(T variant)
  125. {
  126. // Do something with variant.
  127. }