c_sharp_variant.rst 6.3 KB

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