scripting_languages.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. :article_outdated: True
  2. .. Intention: only introduce what a script does in general and options for
  3. scripting languages.
  4. .. _doc_scripting:
  5. Scripting languages
  6. ===================
  7. This lesson will give you an overview of the available scripting languages in
  8. Godot. You will learn the pros and cons of each option. In the next part, you
  9. will write your first script using GDScript.
  10. **Scripts attach to a node and extend its behavior**. This means that scripts
  11. inherit all functions and properties of the node they attach to.
  12. For example, take a game where a Camera2D node follows a ship. The Camera2D node
  13. follows its parent by default. Imagine you want the camera to shake when the player
  14. takes damage. As this feature is not built into Godot, you would attach a script
  15. to the Camera2D node and code the shake.
  16. .. image:: img/scripting_camera_shake.gif
  17. Available scripting languages
  18. -----------------------------
  19. Godot offers **four gameplay programming languages**: GDScript, C#,
  20. and, via its GDExtension technology, C and C++. There are more
  21. community-supported languages, but these are the official ones.
  22. You can use multiple languages in a single project. For instance, in a team, you
  23. could code gameplay logic in GDScript as it's fast to write, and use C# or C++ to
  24. implement complex algorithms and maximize their performance. Or you can write
  25. everything in GDScript or C#. It's your call.
  26. We provide this flexibility to answer the needs of different game projects and
  27. developers.
  28. Which language should I use?
  29. ----------------------------
  30. If you're a beginner, we recommend to **start with GDScript**. We made this
  31. language specifically for Godot and the needs of game developers. It has a
  32. lightweight and straightforward syntax and provides the tightest integration
  33. with Godot.
  34. .. image:: img/scripting_gdscript.png
  35. For C#, you will need an external code editor like
  36. `VSCode <https://code.visualstudio.com/>`_ or Visual Studio. While C# support is
  37. now mature, you will find fewer learning resources for it compared to
  38. GDScript. That's why we recommend C# mainly to users who already have experience
  39. with the language.
  40. Let's look at each language's features, as well as its pros and cons.
  41. GDScript
  42. ~~~~~~~~
  43. :ref:`GDScript<doc_gdscript>` is an
  44. `object-oriented <https://en.wikipedia.org/wiki/Object-oriented_programming>`_ and
  45. `imperative <https://en.wikipedia.org/wiki/Imperative_programming>`_
  46. programming language built for Godot. It's made by and for game developers
  47. to save you time coding games. Its features include:
  48. - A simple syntax that leads to short files.
  49. - Blazing fast compilation and loading times.
  50. - Tight editor integration, with code completion for nodes, signals, and more
  51. information from the scene it's attached to.
  52. - Built-in vector and transform types, making it efficient for heavy use of
  53. linear algebra, a must for games.
  54. - Supports multiple threads as efficiently as statically typed languages.
  55. - No `garbage collection
  56. <https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)>`_, as
  57. this feature eventually gets in the way when creating games. The engine counts
  58. references and manages the memory for you in most cases by default, but you
  59. can also control memory if you need to.
  60. - `Gradual typing <https://en.wikipedia.org/wiki/Gradual_typing>`_. Variables
  61. have dynamic types by default, but you also can use type hints for strong type
  62. checks.
  63. GDScript looks like Python as you structure your code blocks using indentations,
  64. but it doesn't work the same way in practice. It's inspired by multiple
  65. languages, including Squirrel, Lua, and Python.
  66. .. note::
  67. Why don't we use Python or Lua directly?
  68. Years ago, Godot used Python, then Lua. Both languages' integration took a
  69. lot of work and had severe limitations. For example, threading support was a
  70. big challenge with Python.
  71. Developing a dedicated language doesn't take us more work and we can tailor
  72. it to game developers' needs. We're now working on performance optimizations
  73. and features that would've been difficult to offer with third-party
  74. languages.
  75. .NET / C#
  76. ~~~~~~~~~
  77. As Microsoft's `C#
  78. <https://en.wikipedia.org/wiki/C_Sharp_(programming_language)>`_ is a favorite
  79. amongst game developers, we officially support it. C# is a mature and flexible
  80. language with tons of libraries written for it. We were able to add support for it
  81. thanks to a generous donation from Microsoft.
  82. .. image:: img/scripting_csharp.png
  83. C# offers a good tradeoff between performance and ease of use, although you
  84. should be aware of its garbage collector.
  85. .. note:: You must use the .NET edition of the Godot editor to script in C#. You
  86. can download it on the Godot website's `download
  87. <https://godotengine.org/download/>`_ page.
  88. Since Godot uses .NET 6, in theory, you can use any third-party .NET library or
  89. framework in Godot, as well as any Common Language Infrastructure-compliant
  90. programming language, such as F#, Boo, or ClojureCLR. However, C# is the only
  91. officially supported .NET option.
  92. .. note:: GDScript code itself doesn't execute as fast as compiled C# or C++.
  93. However, most script code calls functions written with fast algorithms
  94. in C++ code inside the engine. In many cases, writing gameplay logic
  95. in GDScript, C#, or C++ won't have a significant impact on
  96. performance.
  97. .. attention::
  98. Projects written in C# using Godot 4 currently cannot be exported to the web
  99. platform. To use C# on that platform, consider Godot 3 instead.
  100. Android and iOS platform support is available as of Godot 4.2, but is
  101. experimental and :ref:`some limitations apply <doc_c_sharp_platforms>`.
  102. .. seealso:: To learn more about C#, head to the :ref:`C# basics <doc_c_sharp>` page.
  103. C++ via GDExtension
  104. ~~~~~~~~~~~~~~~~~~~
  105. GDExtension allows you to write game code in C++ without needing to recompile
  106. Godot.
  107. .. image:: img/scripting_cpp.png
  108. You can use any version of the language or mix compiler brands and versions for
  109. the generated shared libraries, thanks to our use of an internal C API Bridge.
  110. GDExtension is the best choice for performance. You don't need to use it
  111. throughout an entire game, as you can write other parts in GDScript or C#.
  112. When working with GDExtension, the available types, functions, and properties
  113. closely resemble Godot's actual C++ API.
  114. Summary
  115. -------
  116. Scripts are files containing code that you attach to a node to extend its
  117. functionality.
  118. Godot supports four official scripting languages, offering you flexibility
  119. between performance and ease of use.
  120. You can mix languages, for instance, to implement demanding algorithms with C or
  121. C++ and write most of the game logic with GDScript or C#.