@@ -9,17 +9,67 @@ For a detailed explanation of Variant in general, see the :ref:`Variant <class_V
99We recommend avoiding ``Godot.Variant `` unless it is necessary to interact with untyped engine APIs.
1010Take advantage of C#'s type safety when possible.
1111
12- Any of ``Variant.As{TYPE} `` methods or the generic ``Variant.As<T> `` method can be used to convert
13- a ``Godot.Variant `` to a C# type. Since the ``Godot.Variant `` type contains implicit conversions
14- defined for all the supported types, calling these methods directly is usually not necessary.
12+ Converting from a supported type to ``Godot.Variant `` can be done using implicit conversions. Also
13+ available are ``CreateFrom `` method overloads and the generic ``Variant.From<T> `` methods.
1514
16- Use ``CreateFrom `` method overloads or the generic ``Variant.From<T> `` method to convert a C# type
17- to a ``Godot.Variant ``.
15+ .. code-block :: csharp
16+
17+ int x = 42 ;
18+ Godot .Variant number = x ;
19+ Godot .Variant hello = " Hello, World!" ;
20+
21+ Godot .Variant number2 = Godot .Variant .CreateFrom (x );
22+ Godot .Variant number3 = Godot .Variant .From (x );
23+
24+ Implicit conversions to ``Godot.Variant `` make passing variants as method arguments very convenient.
25+ For example, ``final_val `` of :ref: `tween_property<class_Tween_method_tween_property> `.
26+
27+ .. code-block :: csharp
28+
29+ Tween tween = CreateTween ();
30+ tween .TweenProperty (GetNode (" Sprite" ), " modulate" , Colors .Red , 1 . 0 f );
31+
32+ Converting from ``Godot.Variant `` to a supported type can be done using explicit conversions. Also
33+ available are ``Variant.As{TYPE} `` methods or the generic ``Variant.As<T> `` method.
34+
35+ .. code-block :: csharp
36+
37+ int cSharpNumber = (int )number ;
38+ string cSharpHello = (string )hello ;
39+
40+ int cSharpNumber2 = number .As <int >();
41+ string cSharpHello2 = hello .AsString ();
42+
43+ .. note ::
44+
45+ All methods of converting from ``Godot.Variant `` to a C# type behave the same way. If conversion
46+ is not possible, the default value of the target type or an empty array is returned. An
47+ exception is not thrown.
48+
49+ A matching type is not necessary for a conversion to succeed. For example, all variants are
50+ convertible to ``string ``.
51+
52+ .. code-block :: csharp
53+
54+ string s = Variant .From (new Vector3 (1 , 3 , 9 )).As <string >();
55+ GD .Print (s ); // (1, 3, 9)
56+
57+ Some C# types are not directly represented by ``Variant.Type ``. The ``Variant.As{TYPE} `` method uses
58+ a more specific name to represent them.
59+
60+ .. code-block :: csharp
61+
62+ int cSharpNumber3 = number .AsInt32 ();
63+
64+ To convert ``Godot.Variant `` to a C# ``object `` without specifying a more specific target type, use
65+ the ``Variant.Obj `` property. This can be used to compare two variants that both have unknown types
66+ using the equality operator ``== ``. When possible, prefer more specific conversions. ``Variant.Obj ``
67+ may result in unnecessary boxing of value types.
1868
1969.. note ::
2070
2171 Since the Variant type in C# is a struct, it can't be null. To create a "null"
22- Variant use the ``default `` keyword or the parameterless constructor.
72+ Variant, use the ``default `` keyword or `` Godot.Variant `` parameterless constructor.
2373
2474Variant-compatible types
2575------------------------
@@ -79,7 +129,7 @@ Variant.Type C# Type
79129
80130 Godot uses 64-bit integers and floats in Variant. Smaller integer and float types
81131 such as ``int ``, ``short `` and ``float `` are supported since they can fit in the
82- bigger type. Be aware that an implicit conversion is performed so using the wrong
132+ bigger type. Be aware that when a conversion is performed, using the wrong
83133 type will result in potential precision loss.
84134
85135.. warning ::
0 commit comments