Skip to content

Commit 2f94e1f

Browse files
committed
Changes and fixes from review
1 parent 4c39a88 commit 2f94e1f

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

tutorials/scripting/c_sharp/c_sharp_variant.rst

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,63 @@ 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-
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.
12+
Converting from a Variant-compatible C# type to ``Godot.Variant`` can be done using implicit
13+
conversions. Also available are ``CreateFrom`` method overloads and the generic ``Variant.From<T>``
14+
methods. Only the syntax is different: the behavior is the same.
1415

1516
.. code-block:: csharp
1617
1718
int x = 42;
18-
Godot.Variant number = x;
19-
Godot.Variant hello = "Hello, World!";
19+
Variant numberVariant = x;
20+
Variant helloVariant = "Hello, World!";
2021
21-
Godot.Variant number2 = Godot.Variant.CreateFrom(x);
22-
Godot.Variant number3 = Godot.Variant.From(x);
22+
Variant numberVariant2 = Variant.CreateFrom(x);
23+
Variant numberVariant3 = Variant.From(x);
2324
2425
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+
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``.
2628

2729
.. code-block:: csharp
2830
2931
Tween tween = CreateTween();
3032
tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f);
3133
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+
Converting from ``Godot.Variant`` to a C# type can be done using explicit conversions. Also
35+
available are ``Variant.As{TYPE}`` methods or the generic ``Variant.As<T>`` method. All of these
36+
behave the same.
3437

3538
.. code-block:: csharp
3639
37-
int cSharpNumber = (int)number;
38-
string cSharpHello = (string)hello;
40+
int number = (int)numberVariant;
41+
string hello = (string)helloVariant;
3942
40-
int cSharpNumber2 = number.As<int>();
41-
string cSharpHello2 = hello.AsString();
43+
int number2 = numberVariant.As<int>();
44+
int number3 = numberVariant.AsInt32();
4245
4346
.. note::
4447

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+
The ``Variant.As{TYPE}`` methods are typically named after C# types (``Int32``), not C# keywords
49+
(``int``).
4850

49-
A matching type is not necessary for a conversion to succeed. For example, all variants are
50-
convertible to ``string``.
51+
If the Variant type doesn't match the conversion target type, the consequences vary depending on the
52+
source and target values.
5153

52-
.. code-block:: csharp
53-
54-
string s = Variant.From(new Vector3(1, 3, 9)).As<string>();
55-
GD.Print(s); // (1, 3, 9)
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.
5659

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
60+
Converting to the correct type avoids complicated behavior and should be preferred.
6161

62-
int cSharpNumber3 = number.AsInt32();
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 may be boxed when it normally wouldn't be.
6366

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.
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.
6869

6970
.. note::
7071

0 commit comments

Comments
 (0)