c_sharp_differences.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. .. _doc_c_sharp_differences:
  2. 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`` in GDScript and C++.
  9. Global Scope
  10. ------------
  11. Available under ``Godot.GD``.
  12. Some things were moved to their own classes, like Math and Random. See below.
  13. Global functions like ``print``, ``var2str`` and ``weakref`` are located under
  14. ``GD`` in C#.
  15. ``ERR_*`` constants were moved to ``Godot.Error``.
  16. Math
  17. ----
  18. Math functions like ``abs``, ``acos``, ``asin``, ``atan`` and ``atan2`` are
  19. located under ``Mathf`` instead of in global scope.
  20. ``PI`` is ``Mathf.PI``
  21. Random
  22. ------
  23. Random functions like ``rand_range`` and ``rand_seed`` are located under ``Random``,
  24. so use ``Random.RandRange`` instead of ``rand_range``.
  25. Export keyword
  26. --------------
  27. Use the ``[Export]`` attribute instead of the GDScript ``export`` keyword.
  28. Signal keyword
  29. --------------
  30. Use the ``[Signal]`` attribute instead of the GDScript ``signal`` keyword.
  31. This attribute should be used on a `delegate`, whose name signature will be used to define the signal.
  32. .. code-block:: csharp
  33. [Signal]
  34. delegate void MySignal(string willSendsAString);
  35. See also: :ref:`c_sharp_signals`
  36. Singletons
  37. ----------
  38. Singletons provide static methods rather than using the singleton pattern in C#.
  39. This is to make code less verbose and similar to GDScript. Example:
  40. .. code-block:: csharp
  41. Input.IsActionPressed("ui_down")
  42. String
  43. ------
  44. Use ``System.String`` (``string``). All the Godot String methods are provided
  45. by the ``StringExtensions`` class as extension methods. Example:
  46. .. code-block:: csharp
  47. string upper = "I LIKE SALAD FORKS";
  48. string lower = upper.ToLower();
  49. There are a few differences though:
  50. * ``erase``: Strings are immutable in C#, so we cannot modify the string
  51. passed to the extension method. For this reason ``Erase`` was added as an
  52. extension method of ``StringBuilder`` instead of string.
  53. Alternatively you can use ``string.Remove``.
  54. * ``IsSubsequenceOf``/``IsSubsequenceOfi``: An additional method is provided
  55. which is an overload of ``IsSubsequenceOf`` allowing to explicitly specify
  56. case sensitivity:
  57. .. code-block:: csharp
  58. str.IsSubsequenceOf("ok"); // Case sensitive
  59. str.IsSubsequenceOf("ok", true); // Case sensitive
  60. str.IsSubsequenceOfi("ok"); // Case insensitive
  61. str.IsSubsequenceOf("ok", false); // Case insensitive
  62. * ``Match``/``Matchn``/``ExprMatch``: An additional method is provided besides
  63. ``Match`` and ``Matchn``, which allows to explicitly specify case sensitivity:
  64. .. code-block:: csharp
  65. str.Match("*.txt"); // Case sensitive
  66. str.ExprMatch("*.txt", true); // Case sensitive
  67. str.Matchn("*.txt"); // Case insensitive
  68. str.ExprMatch("*.txt", false); // Case insensitive
  69. Basis
  70. -----
  71. Structs cannot have parameterless constructors in C#, therefore ``new Basis()``
  72. initializes all primitive members to their default value. Use ``Basis.Identity``
  73. for the equivalent to ``Basis()`` in GDScript and C++.
  74. The following methods were converted to properties with their respective names changed:
  75. ================ ==================================================================
  76. GDScript C#
  77. ================ ==================================================================
  78. get_scale() Scale
  79. ================ ==================================================================
  80. Transform2D
  81. -----------
  82. Structs cannot have parameterless constructors in C#, therefore ``new Transform2D()``
  83. initializes all primitive members to their default value.
  84. Please use ``Transform2D.Identity`` for the equivalent to ``Transform2D()`` in GDScript and C++.
  85. The following methods were converted to properties with their respective names changed:
  86. ================ ==================================================================
  87. GDScript C#
  88. ================ ==================================================================
  89. get_origin() Origin
  90. get_rotation() Rotation
  91. get_scale() Scale
  92. ================ ==================================================================
  93. Plane
  94. -----
  95. The following methods were converted to properties with their respective names changed:
  96. ================ ==================================================================
  97. GDScript C#
  98. ================ ==================================================================
  99. center() Center
  100. ================ ==================================================================
  101. Rect2
  102. -----
  103. The following fields were converted to properties with their respective names changed:
  104. ================ ==================================================================
  105. GDScript C#
  106. ================ ==================================================================
  107. end End
  108. ================ ==================================================================
  109. The following methods were converted to properties with their respective names changed:
  110. ================ ==================================================================
  111. GDScript C#
  112. ================ ==================================================================
  113. get_area() Area
  114. ================ ==================================================================
  115. Quat
  116. ----
  117. Structs cannot have parameterless constructors in C#, therefore ``new Quat()``
  118. initializes all primitive members to their default value.
  119. Please use ``Quat.Identity`` for the equivalent to ``Quat()`` in GDScript and C++.
  120. Array
  121. -----
  122. *This is temporary. Array is ref-counted, so it will need its own type that wraps the native side.
  123. PoolArrays will also need their own type to be used the way they are meant to.*
  124. ================ ==================================================================
  125. GDScript C#
  126. ================ ==================================================================
  127. Array object[]
  128. PoolIntArray int[]
  129. PoolByteArray byte[]
  130. PoolFloatArray float[]
  131. PoolStringArray String[]
  132. PoolColorArray Color[]
  133. PoolVector2Array Vector2[]
  134. PoolVector3Array Vector3[]
  135. ================ ==================================================================
  136. In some exceptional cases a raw array (``type[]``) may be required instead of a ``List``.
  137. Dictionary
  138. ----------
  139. *This is temporary. Array is ref-counted, so it will need its own type that wraps the native side.*
  140. Use ``Dictionary<object, object>``.
  141. Variant
  142. -------
  143. ``System.Object`` (``object``) is used in place of ``Variant``.
  144. Communicating with other scripting languages
  145. --------------------------------------------
  146. The methods ``object Object.call(string method, params object[] args)``,
  147. ``object Object.get(string field)`` and ``object Object.set(string field, object value)``
  148. are provided to communicate with instances of other
  149. scripting languages via the Variant API.
  150. Other differences
  151. -----------------
  152. ``preload``, ``assert`` and ``yield`` as they work in GDScript are currently
  153. not available in C#.
  154. Other differences:
  155. ================ ==================================================================
  156. GDScript C#
  157. ================ ==================================================================
  158. Color8 Color.Color8
  159. is_inf float.IsInfinity
  160. is_nan float.IsNaN
  161. dict2inst ? TODO
  162. inst2dict ? TODO
  163. load GD.load which is the same as ResourceLoader.load
  164. ================ ==================================================================