scripting.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 into *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 extend 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, 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. resulting in a more than sufficient performance 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. the replacement of a GDScript class with a C++ class without altering
  51. the 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, it will not take
  56. more than a few minutes to get an overview of the concepts.
  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/scripting_scene_tree.png
  66. Use the 2D editor to position and resize the button and label so that they
  67. look like the image below. You can set the text in the Inspector pane.
  68. .. image:: /img/label_button_example.png
  69. Finally, save the scene, a fitting name could be "sayhello.scn"
  70. .. _doc_scripting-adding_a_script:
  71. Adding a script
  72. ~~~~~~~~~~~~~~~
  73. Right click on the panel node, then select "Add Script" in the context
  74. menu:
  75. .. image:: /img/add_script.png
  76. The script creation dialog will pop up. This dialog allows to select
  77. the language, class name, etc. GDScript does not use class names in
  78. script files, so that field is not editable. The script should inherit
  79. from "Panel" (as it is meant to extend the node, which is of Panel type,
  80. this is automatically filled).
  81. Enter a path name for the script and then select "Create":
  82. .. image:: /img/script_create.png
  83. Once this is done, the script will be created and added to the node. You
  84. can see this both as an extra icon in the node, as well as in the script
  85. property:
  86. .. image:: /img/script_added.png
  87. To edit the script, select either of the highlighted buttons.
  88. This will bring you to the script editor where an existing template will
  89. be included by default:
  90. .. image:: /img/script_template.png
  91. There is not much in there. The "_ready()" function is called when the
  92. node (and all its children) entered the active scene. (Remember, it's
  93. not a constructor, the constructor is "_init()" ).
  94. The role of the script
  95. ~~~~~~~~~~~~~~~~~~~~~~
  96. A script adds behavior to a node. It is used to control the
  97. node functions as well as other nodes (children, parent, siblings, etc).
  98. The local scope of the script is the node (just like in regular
  99. inheritance) and the virtual functions of the node are captured by the
  100. script.
  101. .. image:: /img/brainslug.jpg
  102. Handling a signal
  103. ~~~~~~~~~~~~~~~~~
  104. Signals are used mostly in GUI nodes (although other nodes have them
  105. too). Signals are "emitted" when some specific kind of action happens,
  106. and can be connected to any function of any script instance. In this
  107. step, the "pressed" signal from the button will be connected to a custom
  108. function.
  109. An interface for connecting signals to your scripts exists in the editor.
  110. You can access this by selecting the node in the scene tree and then
  111. selecting the "Node" tab. Make sure that you have "Signals" selected.
  112. .. image:: /img/signals.png
  113. In any case, at this point it is clear that we are interested in
  114. the "pressed" signal. Instead of using the visual interface, we will opt
  115. to code the connection.
  116. For this, a function exists that is probably the one most used by Godot
  117. programmers, namely :ref:`Node.get_node() <class_Node_get_node>`.
  118. This function uses paths to fetch nodes in the current tree or anywhere
  119. in the scene, relative to the node holding the script.
  120. To fetch the button, the following must be used:
  121. ::
  122. get_node("Button")
  123. Next, a callback will be added that will change the label's text when
  124. the button is pressed:
  125. ::
  126. func _on_button_pressed():
  127. get_node("Label").set_text("HELLO!")
  128. Finally, the button "pressed" signal will be connected to that callback
  129. in _ready(), by using :ref:`Object.connect() <class_Object_connect>`.
  130. ::
  131. func _ready():
  132. get_node("Button").connect("pressed",self,"_on_button_pressed")
  133. The final script should look like this:
  134. ::
  135. extends Panel
  136. # member variables here, example:
  137. # var a=2
  138. # var b="textvar"
  139. func _on_button_pressed():
  140. get_node("Label").set_text("HELLO!")
  141. func _ready():
  142. get_node("Button").connect("pressed",self,"_on_button_pressed")
  143. Running the scene should have the expected result when pressing the
  144. button:
  145. .. image:: /img/scripting_hello.png
  146. **Note:** As it is a common misunderstanding in this tutorial, let's clarify
  147. again that get_node(path) works by returning the *immediate* children of
  148. the node controlled by the script (in this case, *Panel*), so *Button*
  149. must be a child of *Panel* for the above code to work. To give this
  150. clarification more context, if *Button* were a child of *Label*, the code
  151. to obtain it would be:
  152. ::
  153. # not for this case
  154. # but just in case
  155. get_node("Label/Button")
  156. Also, remember that nodes are referenced by name, not by type.