c_sharp_differences.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. .. _doc_c_sharp_differences:
  2. C# API differences to GDScript
  3. ==============================
  4. This is a (incomplete) list of API differences between C# and GDScript.
  5. General differences
  6. -------------------
  7. As explained in the :ref:`doc_c_sharp`, C# generally uses ``PascalCase`` instead
  8. of the ``snake_case`` used in GDScript and C++.
  9. Global scope
  10. ------------
  11. Global functions and some constants had to be moved to classes, since C#
  12. does not allow declaring them in namespaces.
  13. Most global constants were moved to their own enums.
  14. Constants
  15. ^^^^^^^^^
  16. In C#, only primitive types can be constant. For example, the ``TAU`` constant
  17. is replaced by the ``Mathf.Tau`` constant, but the ``Vector2.RIGHT`` constant
  18. is replaced by the ``Vector2.Right`` read-only property. This behaves similarly
  19. to a constant, but can't be used in some contexts like ``switch`` statements.
  20. Global enum constants were moved to their own enums.
  21. For example, ``ERR_*`` constants were moved to the ``Error`` enum.
  22. Special cases:
  23. ======================= ===========================================================
  24. GDScript C#
  25. ======================= ===========================================================
  26. ``SPKEY`` ``GD.SpKey``
  27. ``TYPE_*`` ``Variant.Type`` enum
  28. ``OP_*`` ``Variant.Operator`` enum
  29. ======================= ===========================================================
  30. Math functions
  31. ^^^^^^^^^^^^^^
  32. Math global functions, like ``abs``, ``acos``, ``asin``, ``atan`` and ``atan2``, are
  33. located under ``Mathf`` as ``Abs``, ``Acos``, ``Asin``, ``Atan`` and ``Atan2``.
  34. The ``PI`` constant can be found as ``Mathf.Pi``.
  35. Random functions
  36. ^^^^^^^^^^^^^^^^
  37. Random global functions, like ``rand_range`` and ``rand_seed``, are located under ``GD``.
  38. Example: ``GD.RandRange`` and ``GD.RandSeed``.
  39. Other functions
  40. ^^^^^^^^^^^^^^^
  41. Many other global functions like ``print`` and ``var2str`` are located under ``GD``.
  42. Example: ``GD.Print`` and ``GD.Var2Str``.
  43. Exceptions:
  44. =========================== =======================================================
  45. GDScript C#
  46. =========================== =======================================================
  47. ``weakref(obj)`` ``Object.WeakRef(obj)``
  48. ``is_instance_valid(obj)`` ``Object.IsInstanceValid(obj)``
  49. =========================== =======================================================
  50. Tips
  51. ^^^^
  52. Sometimes it can be useful to use the ``using static`` directive. This directive allows
  53. to access the members and nested types of a class without specifying the class name.
  54. Example:
  55. .. code-block:: csharp
  56. using static Godot.GD;
  57. public class Test
  58. {
  59. static Test()
  60. {
  61. Print("Hello"); // Instead of GD.Print("Hello");
  62. }
  63. }
  64. Export keyword
  65. --------------
  66. Use the ``[Export]`` attribute instead of the GDScript ``export`` keyword.
  67. This attribute can also be provided with optional :ref:`PropertyHint<enum_@GlobalScope_PropertyHint>` and ``hintString`` parameters.
  68. Default values can be set by assigning a value.
  69. Example:
  70. .. code-block:: csharp
  71. using Godot;
  72. public class MyNode : Node
  73. {
  74. [Export]
  75. private NodePath _nodePath;
  76. [Export]
  77. private string _name = "default";
  78. [Export(PropertyHint.Range, "0,100000,1000,or_greater")]
  79. private int _income;
  80. [Export(PropertyHint.File, "*.png,*.jpg")]
  81. private string _icon;
  82. }
  83. Signal keyword
  84. --------------
  85. Use the ``[Signal]`` attribute to declare a signal instead of the GDScript ``signal`` keyword.
  86. This attribute should be used on a `delegate`, whose name signature will be used to define the signal.
  87. The `delegate` must have the ``EventHandler`` suffix, an `event` will be generated in the class with the same name but without the suffix, use that event's name with ``EmitSignal``.
  88. .. code-block:: csharp
  89. [Signal]
  90. delegate void MySignalEventHandler(string willSendAString);
  91. See also: :ref:`doc_c_sharp_signals`.
  92. `@onready` annotation
  93. ---------------------
  94. GDScript has the ability to defer the initialization of a member variable until the ready function
  95. is called with `@onready` (cf. :ref:`doc_gdscript_onready_annotation`).
  96. For example:
  97. .. code-block:: gdscript
  98. @onready var my_label = get_node("MyLabel")
  99. However C# does not have this ability. To achieve the same effect you need to do this.
  100. .. code-block:: csharp
  101. private Label _myLabel;
  102. public override void _Ready()
  103. {
  104. _myLabel = GetNode<Label>("MyLabel");
  105. }
  106. Singletons
  107. ----------
  108. Singletons are available as static classes rather than using the singleton pattern.
  109. This is to make code less verbose than it would be with an ``Instance`` property.
  110. Example:
  111. .. code-block:: csharp
  112. Input.IsActionPressed("ui_down")
  113. However, in some very rare cases this is not enough. For example, you may want
  114. to access a member from the base class ``Godot.Object``, like ``Connect``.
  115. For such use cases we provide a static property named ``Singleton`` that returns
  116. the singleton instance. The type of this instance is ``Godot.Object``.
  117. Example:
  118. .. code-block:: csharp
  119. Input.Singleton.JoyConnectionChanged += Input_JoyConnectionChanged;
  120. String
  121. ------
  122. Use ``System.String`` (``string``). Most of Godot's String methods are
  123. provided by the ``StringExtensions`` class as extension methods.
  124. Example:
  125. .. code-block:: csharp
  126. string upper = "I LIKE SALAD FORKS";
  127. string lower = upper.ToLower();
  128. There are a few differences, though:
  129. * ``erase``: Strings are immutable in C#, so we cannot modify the string
  130. passed to the extension method. For this reason, ``Erase`` was added as an
  131. extension method of ``StringBuilder`` instead of string.
  132. Alternatively, you can use ``string.Remove``.
  133. * ``IsSubsequenceOf``/``IsSubsequenceOfi``: An additional method is provided,
  134. which is an overload of ``IsSubsequenceOf``, allowing you to explicitly specify
  135. case sensitivity:
  136. .. code-block:: csharp
  137. str.IsSubsequenceOf("ok"); // Case sensitive
  138. str.IsSubsequenceOf("ok", true); // Case sensitive
  139. str.IsSubsequenceOfi("ok"); // Case insensitive
  140. str.IsSubsequenceOf("ok", false); // Case insensitive
  141. * ``Match``/``Matchn``/``ExprMatch``: An additional method is provided besides
  142. ``Match`` and ``Matchn``, which allows you to explicitly specify case sensitivity:
  143. .. code-block:: csharp
  144. str.Match("*.txt"); // Case sensitive
  145. str.ExprMatch("*.txt", true); // Case sensitive
  146. str.Matchn("*.txt"); // Case insensitive
  147. str.ExprMatch("*.txt", false); // Case insensitive
  148. Basis
  149. -----
  150. Structs cannot have parameterless constructors in C#. Therefore, ``new Basis()``
  151. initializes all primitive members to their default value. Use ``Basis.Identity``
  152. for the equivalent of ``Basis()`` in GDScript and C++.
  153. The following method was converted to a property with a different name:
  154. ==================== ==============================================================
  155. GDScript C#
  156. ==================== ==============================================================
  157. ``get_scale()`` ``Scale``
  158. ==================== ==============================================================
  159. Transform2D
  160. -----------
  161. Structs cannot have parameterless constructors in C#. Therefore, ``new Transform2D()``
  162. initializes all primitive members to their default value.
  163. Please use ``Transform2D.Identity`` for the equivalent of ``Transform2D()`` in GDScript and C++.
  164. The following methods were converted to properties with their respective names changed:
  165. ==================== ==============================================================
  166. GDScript C#
  167. ==================== ==============================================================
  168. ``get_rotation()`` ``Rotation``
  169. ``get_scale()`` ``Scale``
  170. ==================== ==============================================================
  171. Plane
  172. -----
  173. The following method was converted to a property with a *slightly* different name:
  174. ================ ==================================================================
  175. GDScript C#
  176. ================ ==================================================================
  177. ``center()`` ``Center``
  178. ================ ==================================================================
  179. Rect2
  180. -----
  181. The following field was converted to a property with a *slightly* different name:
  182. ================ ==================================================================
  183. GDScript C#
  184. ================ ==================================================================
  185. ``end`` ``End``
  186. ================ ==================================================================
  187. The following method was converted to a property with a different name:
  188. ================ ==================================================================
  189. GDScript C#
  190. ================ ==================================================================
  191. ``get_area()`` ``Area``
  192. ================ ==================================================================
  193. Quaternion
  194. ----------
  195. Structs cannot have parameterless constructors in C#. Therefore, ``new Quaternion()``
  196. initializes all primitive members to their default value.
  197. Please use ``Quaternion.Identity`` for the equivalent of ``Quaternion()`` in GDScript and C++.
  198. The following methods were converted to a property with a different name:
  199. ===================== =============================================================
  200. GDScript C#
  201. ===================== =============================================================
  202. ``length()`` ``Length``
  203. ``length_squared()`` ``LengthSquared``
  204. ===================== =============================================================
  205. Array
  206. -----
  207. *This is temporary. PackedArrays will need their own types to be used the way they are meant to.*
  208. ====================== ==============================================================
  209. GDScript C#
  210. ====================== ==============================================================
  211. ``Array`` ``Godot.Collections.Array``
  212. ``PackedInt32Array`` ``int[]``
  213. ``PackedInt64Array`` ``long[]``
  214. ``PackedByteArray`` ``byte[]``
  215. ``PackedFloat32Array`` ``float[]``
  216. ``PackedFloat64Array`` ``double[]``
  217. ``PackedStringArray`` ``String[]``
  218. ``PackedColorArray`` ``Color[]``
  219. ``PackedVector2Array`` ``Vector2[]``
  220. ``PackedVector3Array`` ``Vector3[]``
  221. ====================== ==============================================================
  222. ``Godot.Collections.Array<T>`` is a type-safe wrapper around ``Godot.Collections.Array``.
  223. Use the ``Godot.Collections.Array<T>(Godot.Collections.Array)`` constructor to create one.
  224. Dictionary
  225. ----------
  226. Use ``Godot.Collections.Dictionary``.
  227. ``Godot.Collections.Dictionary<T>`` is a type-safe wrapper around ``Godot.Collections.Dictionary``.
  228. Use the ``Godot.Collections.Dictionary<T>(Godot.Collections.Dictionary)`` constructor to create one.
  229. Variant
  230. -------
  231. ``System.Object`` (``object``) is used instead of ``Variant``.
  232. Communicating with other scripting languages
  233. --------------------------------------------
  234. This is explained extensively in :ref:`doc_cross_language_scripting`.
  235. Yield
  236. -----
  237. Something similar to GDScript's ``yield`` with a single parameter can be achieved with
  238. C#'s `yield keyword <https://docs.microsoft.com/en-US/dotnet/csharp/language-reference/keywords/yield>`_.
  239. The equivalent of yield on signal can be achieved with async/await and ``Godot.Object.ToSignal``.
  240. Example:
  241. .. code-block:: csharp
  242. await ToSignal(timer, "timeout");
  243. GD.Print("After timeout");
  244. Other differences
  245. -----------------
  246. ``preload``, as it works in GDScript, is not available in C#.
  247. Use ``GD.Load`` or ``ResourceLoader.Load`` instead.
  248. Other differences:
  249. ================ ==================================================================
  250. GDScript C#
  251. ================ ==================================================================
  252. ``Color8`` ``Color.Color8``
  253. ``is_inf`` ``float.IsInfinity``
  254. ``is_nan`` ``float.IsNaN``
  255. ``dict2inst`` TODO
  256. ``inst2dict`` TODO
  257. ================ ==================================================================