scripting.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. .. _doc_scripting:
  2. Scripting
  3. =========
  4. Introduction
  5. ------------
  6. Much has been said about tools that allow users to create video games
  7. without programming. It's been a dream for many independent developers
  8. to create games without learning how to code. This need has been around
  9. for a long time, even inside companies, where game designers wish to
  10. have more control of the game flow.
  11. Many products have been shipped promising a no-programming environment,
  12. but the result is often incomplete, too complex or inefficient compared
  13. to traditional code. As a result, programming is here to stay for a long
  14. time. In fact, the general direction in game engines has been to add
  15. tools that try to reduce the amount of code that needs to be written for
  16. specific tasks, to speed up development.
  17. In that sense, Godot has taken some useful design decisions towards that
  18. goal. The first and most important is the scene system. The aim of it is
  19. not obvious at first, but works well later on. That is, to relieve
  20. programmers from the responsibility of architecting code.
  21. When designing games using the scene system, the whole project is
  22. fragmented in *complementary* scenes (not individual ones). Scenes
  23. complement each other, instead of being separate. There will be plenty
  24. of examples about this later on, but it's very important to remember it.
  25. For those with a good amount of programming expertise, this means a
  26. different design pattern to MVC. Godot promises efficiency at the
  27. expense of dropping the MVC habits, which are replaced by the *scenes as
  28. a complement* pattern.
  29. Godot also uses the `extend <http://c2.com/cgi/wiki?EmbedVsExtend>`__
  30. pattern for scripting, meaning that scripts extends from all the
  31. available engine classes.
  32. GDScript
  33. --------
  34. :ref:`doc_gdscript` is a dynamically typed scripting language to fit
  35. inside Godot. It was designed with the following goals:
  36. - First and most importantly, making it simple, familiar and as easy to
  37. learn as possible.
  38. - Making the code readable and error safe. The syntax is mostly
  39. borrowed from Python.
  40. Programmers generally take a few days to learn it, and within two weeks
  41. feel comfortable with it.
  42. As with most dynamically typed languages though, the higher productivity
  43. (code is easier to learn, faster to write, no compilation, etc) is
  44. balanced with a performance penalty, but most critical code is written
  45. in C++ already in the engine (vector ops, physics, math, indexing, etc),
  46. making the resulting performance more than enough for most types of
  47. games.
  48. In any case, if more performance is required, critical sections can be
  49. rewritten in C++ and exposed transparently to the script. This allows
  50. for replacing a GDScript class with a C++ class without altering the
  51. rest of the game.
  52. Scripting a Scene
  53. -----------------
  54. Before continuing, please make sure to read the :ref:`doc_gdscript` reference.
  55. It's a simple language and the reference is short, should not take more
  56. than a few minutes to glance.
  57. Scene Setup
  58. ~~~~~~~~~~~
  59. This tutorial will begin by scripting a simple GUI scene. Use the add
  60. node dialog to create the following hierarchy, with the following nodes:
  61. - Panel
  62. \* Label
  63. \* Button
  64. It should look like this in the scene tree:
  65. .. image:: /img/scriptscene.png
  66. And try to make it look like this in the 2D editor, so it makes sense:
  67. .. image:: /img/scriptsceneimg.png
  68. Finally, save the scene, a fitting name could be "sayhello.scn"
  69. Adding a Script
  70. ~~~~~~~~~~~~~~~
  71. Select the Panel node, then press the "Add Script" Icon as follows:
  72. .. image:: /img/addscript.png
  73. The script creation dialog will popup. This dialog allows to select
  74. the language, class name, etc. GDScript does not use class names in
  75. script files, so that field is not editable. The script should inherit
  76. from "Panel" (as it is meant to extend the node, which is of Panel type,
  77. this is automatically filled anyway).
  78. Select the filename for the script (if you saved the scene previously,
  79. one will be automatically generated as sayhello.gd) and push "Create":
  80. .. image:: /img/scriptcreate.png
  81. Once this is done, the script will be created and added to the node. You
  82. can see this both as an extra icon in the node, as well as in the script
  83. property:
  84. .. image:: /img/scriptadded.png
  85. To edit the script, pushing the icon above should do it (although, the
  86. UI will take you directly to the Script editor screen). So, here's the
  87. template script:
  88. .. image:: /img/script_template.png
  89. There is not much in there. The "\_ready()" function is called when the
  90. node (and all it's children) entered the active scene. (Remember, It's
  91. not a constructor, the constructor is "\_init()" ).
  92. The Role of the Script
  93. ~~~~~~~~~~~~~~~~~~~~~~
  94. A script basically adds a behavior to a node. It is used to control the
  95. node functions as well as other nodes (children, parent, siblings, etc).
  96. The local scope of the script is the node (just like in regular
  97. inheritance) and the virtual functions of the node are captured by the
  98. script.
  99. .. image:: /img/brainslug.jpg
  100. Handling a Signal
  101. ~~~~~~~~~~~~~~~~~
  102. Signals are used mostly in GUI nodes, (although other nodes have them
  103. too). Signals are "emitted" when some specific kind of action happens,
  104. and can be connected to any function of any script instance. In this
  105. step, the "pressed" signal from the button will be connected to a custom
  106. function.
  107. There is a GUI for connecting signals, just select the node and press
  108. the "Signals" button:
  109. .. image:: /img/signals.png
  110. Which will show the list of signals a Button can emit.
  111. .. image:: /img/button_connections.png
  112. But this example will not use it. We don't want to make things *too*
  113. easy. So please close that screen!
  114. In any case, at this point it is clear that that we are interested in
  115. the "pressed" signal, so instead of doing it with the visual
  116. interface, the connection will be done using code.
  117. For this, there is a function that is probably the one that Godot
  118. programmers will use the most, this is
  119. `get\_node() <https://github.com/okamstudio/godot/wiki/class_node#get_node>`__.
  120. This function uses paths to fetch nodes in the current tree or anywhere
  121. in the scene, relative to the node holding the script.
  122. To fetch the button, the following must be used:
  123. ::
  124. get_node("Button")
  125. So, next, a callback will be added for when a button is pressed, that
  126. will change the label's text:
  127. ::
  128. func _on_button_pressed():
  129. get_node("Label").set_text("HELLO!")
  130. Finally, the button "pressed" signal will be connected to that callback
  131. in \_ready(), by using
  132. `connect <https://github.com/okamstudio/godot/wiki/class_object#connect()>`__.
  133. ::
  134. func _ready():
  135. get_node("Button").connect("pressed",self,"_on_button_pressed")
  136. The final script should look like this:
  137. ::
  138. extends Panel
  139. # member variables here, example:
  140. # var a=2
  141. # var b="textvar"
  142. func _on_button_pressed():
  143. get_node("Label").set_text("HELLO!")
  144. func _ready():
  145. get_node("Button").connect("pressed",self,"_on_button_pressed")
  146. Running the scene should have the expected result when pressing the
  147. button:
  148. .. image:: /img/scripthello.png
  149. **Note:** As it is a common mistake in this tutorial, let's clarify
  150. again that get\_node(path) works by returning the immediate children to
  151. the node controlled by the script (in this case, *Panel*), so *Button*
  152. must be a child of *Panel* for the above code to work. To give this
  153. clarification more context, if *Button* was a child of *Label*, the code
  154. to obtain it would be:
  155. ::
  156. # not for this case
  157. # but just in case
  158. get_node("Label/Button")
  159. And, also, try to remember that nodes are referenced by name, not by
  160. type.