c_sharp_differences.rst 7.5 KB

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