scripting_languages.rst 8.2 KB

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