scripting.rst 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. .. _doc_scripting:
  2. Scripting
  3. =========
  4. Introduction
  5. ------------
  6. Before Godot 3.0, the only choice for scripting a game in Godot was to use
  7. :ref:`doc_gdscript`. Nowadays, Godot has four (yes four!) official languages
  8. and the ability to add extra scripting languages dynamically!
  9. This is great, mostly, due the large amount of flexibility provided, but
  10. it also makes our work of supporting languages more difficult.
  11. The "Main" languages in Godot, though, are GDScript and VisualScript. The
  12. main reason to choose them is the level of integration with Godot, as this
  13. makes the experience smoother (both have very slick editor integration, while
  14. C# and C++ need to be edited in a separate IDE). If you are a big fan of statically typed languages, though, go with C# and C++.
  15. There are more languages available via GDNative interface, but keep in mind
  16. we don't have official support for them so you will have to work with the
  17. provided in that case.
  18. GDScript
  19. ~~~~~~~~
  20. :ref:`doc_gdscript` is, as mentioned above, the main language used in Godot.
  21. Using it has some really positive points compared to other languages due
  22. to the high integration with Godot:
  23. * It's simple, elegant and designed to be friendly with users of other languages such as Lua, Python, Squirrel, etc.
  24. * Loads and compiles blazingly fast.
  25. * The editor integration is a pleasure to work with, with code completion expanding as far as completing nodes, signals, and many other items pertaining to the current scene being edited.
  26. * Has vector types built-in (such as Vectors, transorms etc), making it efficient for heavy linear algebra.
  27. * It supports multiple threads as well as statically typed languages (which is one of the limitation that avoided us going for other VMs such as Lua, Squirrel, etc).
  28. * It uses no garbage collector, so it trades a small bit of automation (most
  29. objects are reference counted anyway), by determinism.
  30. * The dynamic nature of it makes it easy to optimize chunks of it in C++ (via GDNative) if more performance is required at some point, without recompiling the engine.
  31. If undecided, and have experience in programming (specially dynamically
  32. typed languages), go for GDScript!
  33. Visual Script
  34. ~~~~~~~~~~~~~
  35. Begining 3.0, Godot offers :ref:`visualscript<Visual Scripting>`. This is a
  36. somewhat typical implementation of blocks and connections language, but
  37. adapted to how Godot works.
  38. Visual Scripting is a great tool for non-programmers, or even for developers
  39. with ample programming experience that want to make parts of the code more
  40. accessible to others (like Game/Level Designers or Artists).
  41. It can also be used by programmers to build state machines or custom
  42. visual node workflows (like a Dialogue System).
  43. Microsoft C#
  44. ~~~~~~~~~~~~
  45. As Microsoft's C# is a favorite amongst game developers, we have added
  46. solid and official support for it. C# is a very mature language with tons of code
  47. written for it. Support for this language was added thanks to a generous
  48. donation from Microsoft.
  49. It has an excellent tradeoff between performance and ease to use, although
  50. the main item one must be aware of is the garbage collector.
  51. For companies, this is usually the best choice, given the large amount of
  52. programmers that can be found in the labor market familiar with it, which
  53. reduces the learning time of the engine.
  54. GDNative C++
  55. ~~~~~~~~~~~~
  56. Finally, this is one of our brightest additions for the 3.0 release.
  57. GDNative allows scripting in C++ without requiring to recompile (or even
  58. restart) Godot.
  59. Any C++ version can be used, and mixing compiler brands and versions for the
  60. generated shared libraries works perfectly, thanks to our use of an internal C
  61. API Bridge.
  62. This language is the best choice for performance and does not need to be
  63. used for an entire game (other parts can be written in GDScript or Visual
  64. Script). Yet, the API is very clear and easy to use as it resembles, mostly,
  65. Godot actual C++ API.
  66. Scripting a scene
  67. -----------------
  68. In the rest of this tutorial, we'll set up a simple GUI scene consisting of a
  69. button and a label, where pressing the button will update the label. This will
  70. demonstrate:
  71. - how to write a basic script and attach it to a node
  72. - how to hook up UI elements via *signals*
  73. - how to write a script that can access other nodes in the scene
  74. Before continuing, please make sure to read the :ref:`doc_gdscript` reference.
  75. It's a simple language and the reference is short, so it will not take more
  76. than a few minutes to get an overview of the concepts.
  77. Scene setup
  78. ~~~~~~~~~~~
  79. Use the add node dialog to create the following hierarchy, with the following
  80. nodes:
  81. - Panel
  82. * Label
  83. * Button
  84. It should look like this in the scene tree:
  85. .. image:: /img/scripting_scene_tree.png
  86. Use the 2D editor to position and resize the button and label so that they
  87. look like the image below. You can set the text in the Inspector pane.
  88. .. image:: /img/label_button_example.png
  89. Finally, save the scene, with a name such as "sayhello.tscn"
  90. .. _doc_scripting-adding_a_script:
  91. Adding a script
  92. ~~~~~~~~~~~~~~~
  93. Right click on the panel node, and then select "Add Script" in the context
  94. menu:
  95. .. image:: /img/add_script.png
  96. The script creation dialog will pop up. This dialog allows you to set the
  97. language, class name, and other relevant options.
  98. Actually, in GDScript, the file itself represents the class, so in this case,
  99. the class name field is not editable.
  100. The node we're attaching the script to is a panel, so the "Inherits" field
  101. should automatically be filled in with "Panel". This is what we want as our
  102. script's goal is to extend this panel node's functionality.
  103. Finally, enter a path name for the script and select "Create":
  104. .. image:: /img/script_create.png
  105. Once this is done, the script will be created and added to the node. You can
  106. see this both as an extra icon in the node as well as in the script property:
  107. .. image:: /img/script_added.png
  108. To edit the script, select either of the highlighted buttons. This will bring
  109. you to the script editor where an existing template will be included by default:
  110. .. image:: /img/script_template.png
  111. There is not much in there. The "_ready()" function is called when the
  112. node (and all its children) enter the active scene. (Note: "_ready()" is not
  113. the a constructor; the constructor is "_init()").
  114. The role of the script
  115. ~~~~~~~~~~~~~~~~~~~~~~
  116. A script adds behavior to a node. It is used to control how the node functions
  117. as well as how it interacts with other nodes (children, parent, siblings,
  118. etc.). The local scope of the script is the node. In other words, the script
  119. inherits the functions provided by that node.
  120. .. image:: /img/brainslug.jpg
  121. Handling a signal
  122. ~~~~~~~~~~~~~~~~~
  123. Signals are "emitted" when some specific kind of action happens, and they can be
  124. connected to any function of any script instance. Signals are used mostly in
  125. GUI nodes (although other nodes have them too, and you can even define custom
  126. signals in your own scripts).
  127. In this step, we'll connect the "pressed" signal to a custom function.
  128. The editor provides an interface for connecting signals to your scripts. You
  129. can access this by selecting the node in the scene tree and then selecting the
  130. "Node" tab. Next, make sure that you have "Signals" selected.
  131. .. image:: /img/signals.png
  132. At this point, you could use the visual interface to hook up the "pressed"
  133. signal by double clicking on it and selecting a target node that already has a
  134. script attached to it. But for the sake of learning, we're going to code up the
  135. connection manually.
  136. To accomplish this, we will introduce a function that is probably the most used
  137. by Godot programmers, namely :ref:`Node.get_node() <class_Node_get_node>`.
  138. This function uses paths to fetch nodes anywhere in the scene, relative to the
  139. node that owns the script.
  140. In our case, because the button and the label are siblings under the panel
  141. where the script is attached, you can fetch the button as follows:
  142. ::
  143. get_node("Button")
  144. Next, write a function which will be called when the button is pressed:
  145. .. tabs::
  146. .. code-tab:: gdscript GDScript
  147. func _on_button_pressed():
  148. get_node("Label").text="HELLO!"
  149. .. code-tab:: csharp
  150. // i dont know how this is supposed to be in C#
  151. .. group-tab:: VS
  152. .. image:: /img/signals.png
  153. Finally, connect the button's "pressed" signal to that callback in _ready(), by
  154. using :ref:`Object.connect() <class_Object_connect>`.
  155. ::
  156. func _ready():
  157. get_node("Button").connect("pressed",self,"_on_button_pressed")
  158. The final script should look basically like this:
  159. ::
  160. extends Panel
  161. func _on_button_pressed():
  162. get_node("Label").text="HELLO!"
  163. func _ready():
  164. get_node("Button").connect("pressed",self,"_on_button_pressed")
  165. Run the scene and press the button. You should get the following result:
  166. .. image:: /img/scripting_hello.png
  167. Why hello there! Congratulations on scripting your first scene.
  168. **Note:** A common misunderstanding in this tutorial is how get_node(path)
  169. works. For some given node, get_node(path) searches its immediate children.
  170. In the above code, this means that *Button* must be a child of *Panel*. If
  171. *Button* were instead a child of *Label*, the code to obtain it would be:
  172. ::
  173. # not for this case
  174. # but just in case
  175. get_node("Label/Button")
  176. Also, remember that nodes are referenced by name, not by type.