2
0

c_sharp_differences.rst 7.2 KB

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