Skip to content

Commit 664b739

Browse files
authored
Merge pull request godotengine#8878 from 31/dev/31/variant-obj
c_sharp_variant.rst: add more examples, fix explicit vs. implicit
2 parents 2167846 + d41f09f commit 664b739

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

tutorials/scripting/c_sharp/c_sharp_variant.rst

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,68 @@ For a detailed explanation of Variant in general, see the :ref:`Variant <class_V
99
We recommend avoiding ``Godot.Variant`` unless it is necessary to interact with untyped engine APIs.
1010
Take 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 Variant-compatible C# type to ``Godot.Variant`` can be done using implicit
13+
conversions. There are also ``CreateFrom`` method overloads and the generic ``Variant.From<T>``
14+
methods. Only the syntax is different: the behavior is the same.
1515

16-
Use ``CreateFrom`` method overloads or the generic ``Variant.From<T>`` method to convert a C# type
17-
to a ``Godot.Variant``.
16+
.. code-block:: csharp
17+
18+
int x = 42;
19+
Variant numberVariant = x;
20+
Variant helloVariant = "Hello, World!";
21+
22+
Variant numberVariant2 = Variant.CreateFrom(x);
23+
Variant numberVariant3 = Variant.From(x);
24+
25+
Implicit conversions to ``Godot.Variant`` make passing variants as method arguments very convenient.
26+
For example, the third argument of :ref:`tween_property<class_Tween_method_tween_property>`
27+
specifying the final color of the tween is a ``Godot.Variant``.
28+
29+
.. code-block:: csharp
30+
31+
Tween tween = CreateTween();
32+
tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f);
33+
34+
Converting from ``Godot.Variant`` to a C# type can be done using explicit conversions. There are
35+
also ``Variant.As{TYPE}`` methods and the generic ``Variant.As<T>`` method. All of these behave the
36+
same.
37+
38+
.. code-block:: csharp
39+
40+
int number = (int)numberVariant;
41+
string hello = (string)helloVariant;
42+
43+
int number2 = numberVariant.As<int>();
44+
int number3 = numberVariant.AsInt32();
45+
46+
.. note::
47+
48+
The ``Variant.As{TYPE}`` methods are typically named after C# types (``Int32``), not C# keywords
49+
(``int``).
50+
51+
If the Variant type doesn't match the conversion target type, the consequences vary depending on the
52+
source and target values.
53+
54+
- The conversion may examine the value and return a similar but potentially unexpected value of the
55+
target type. For example, the string ``"42a"`` may be converted to the integer ``42``.
56+
- The default value of the target type may be returned.
57+
- An empty array may be returned.
58+
- An exception may be thrown.
59+
60+
Converting to the correct type avoids complicated behavior and should be preferred.
61+
62+
The ``Variant.Obj`` property returns a C# ``object`` with the correct value for any variant. This
63+
may be useful when the type of Variant is completely unknown. However, when possible, prefer more
64+
specific conversions. ``Variant.Obj`` evaluates a ``switch`` on ``Variant.VariantType`` and it may
65+
not be necessary. Also, if the result is a value type, it is boxed.
66+
67+
For example, if the potential for ``Variant.As<MyNode>()`` to throw a invalid cast exception isn't
68+
acceptable, consider using a ``Variant.As<GodotObject>() is MyNode n`` type pattern instead.
1869

1970
.. note::
2071

2172
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.
73+
Variant, use the ``default`` keyword or the ``Godot.Variant`` parameterless constructor.
2374

2475
Variant-compatible types
2576
------------------------
@@ -79,7 +130,7 @@ Variant.Type C# Type
79130

80131
Godot uses 64-bit integers and floats in Variant. Smaller integer and float types
81132
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
133+
bigger type. Be aware that when a conversion is performed, using the wrong
83134
type will result in potential precision loss.
84135

85136
.. warning::

0 commit comments

Comments
 (0)