scenes_and_nodes.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. .. _doc_scenes_and_nodes:
  2. Scenes and nodes
  3. ================
  4. Introduction
  5. ------------
  6. .. image:: /img/chef.png
  7. Imagine for a second that you are not a game developer anymore. Instead,
  8. you're a chef! Change your hipster outfit for a toque and a double
  9. breasted jacket. Now, instead of making games, you create new and
  10. delicious recipes for your guests.
  11. So, how does a chef create a recipe? Recipes are divided into two
  12. sections: the first is the ingredients and the second is the
  13. instructions to prepare it. This way, anyone can follow the recipe and
  14. savor your magnificent creation.
  15. Making games in Godot feels pretty much the same way. Using the engine
  16. feels like being in a kitchen. In this kitchen, *nodes* are like a
  17. refrigerator full of fresh ingredients to cook with.
  18. There are many types of nodes, some show images, others play sound,
  19. other nodes display 3D models, etc. There's dozens of them.
  20. Nodes
  21. -----
  22. But let's start with the basics. A node is a basic element for creating a
  23. game, it has the following characteristics:
  24. - Has a name.
  25. - Has editable properties.
  26. - Can receive a callback to process every frame.
  27. - Can be extended (to have more functions).
  28. - Can be added to other nodes as children.
  29. .. image:: /img/tree.png
  30. The last one is very important. Nodes can have other nodes as
  31. children. When arranged in this way, the nodes become a **tree**.
  32. In Godot, the ability to arrange nodes in this way creates a powerful
  33. tool for organizing projects. Since different nodes have different
  34. functions, combining them allows for creation of more complex functions.
  35. This is probably not clear yet and makes little sense, but everything
  36. will click a few sections ahead. The most important fact to remember for
  37. now is that nodes exist and can be arranged this way.
  38. Scenes
  39. ------
  40. .. image:: /img/scene_tree_example.png
  41. Now that the concept of nodes has been defined, the next logical
  42. step is to explain what a Scene is.
  43. A scene is composed of a group of nodes organized hierarchically (in
  44. tree fashion). It has the following properties:
  45. - A scene always has only one root node.
  46. - Scenes can be saved to disk and loaded back.
  47. - Scenes can be *instanced* (more on that later).
  48. - Running a game means running a scene.
  49. - There can be several scenes in a project, but for it to start, one of
  50. them must be selected to be loaded first.
  51. Basically, the Godot editor is a **scene editor**. It has plenty of
  52. tools for editing 2D and 3D scenes as well as user interfaces, but the
  53. editor is based on the concept of editing a scene and the nodes
  54. that compose it.
  55. Creating a new project
  56. ----------------------
  57. Theory is boring, so let's change the subject and go practical. Following a
  58. long tradition in tutorials, the first project will be a Hello World.
  59. For this, the editor will be used.
  60. When the godot executable is run outside a project, the Project Manager
  61. appears. This helps developers manage their projects.
  62. .. image:: /img/project_manager.png
  63. To create a new project, the "New Project" option must be used. Choose
  64. and create a path for the project and specify the project name:
  65. .. image:: /img/create_new_project.png
  66. Editor
  67. ------
  68. Once the "New Project" is created, the next step is opening it. This
  69. will open the Godot editor. Here is how the editor looks when freshly
  70. opened:
  71. .. image:: /img/empty_editor.png
  72. As mentioned before, making games in Godot feels like being in a
  73. kitchen, so let's open the refrigerator and add some fresh nodes to the
  74. project. We'll begin with a Hello World! To do this, the "New Node"
  75. button must be pressed (looks like a plus symbol underneath the "Scene" tab):
  76. .. image:: /img/newnode_button.png
  77. This will open the Create Node dialog, showing the long list of nodes
  78. that can be created:
  79. .. image:: /img/node_classes.png
  80. From there, select the "Label" node first. It is under the "Control" node, but searching for it is probably
  81. the quickest way:
  82. .. image:: /img/node_search_label.png
  83. And finally, create the Label! A lot happens when Create is pressed:
  84. .. image:: /img/editor_with_label.png
  85. First of all, the scene is changed to the 2D editor (because Label is
  86. a 2D Node type), and the Label appears, selected, at the top left
  87. corner of the viewport.
  88. The node appears in the scene tree editor (box in the top left
  89. corner), and the label properties appear in the Inspector (box on the
  90. right side).
  91. The next step will be to change the "Text" Property of the label, let's
  92. change it to "Hello, World!":
  93. .. image:: /img/hw.png
  94. Ok, everything's ready to run the scene! Press the PLAY SCENE Button on
  95. the top bar (or hit F6):
  96. .. image:: /img/playscene.png
  97. Aaaand... Oops.
  98. .. image:: /img/neversaved.png
  99. Scenes need to be saved to be run, so save the scene to something like
  100. hello.scn in Scene -> Save:
  101. .. image:: /img/save_scene.png
  102. And here's when something funny happens. The file dialog is a special
  103. file dialog, and only allows you to save inside the project. The project
  104. root is "res://" which means "resource path". This means that files can
  105. only be saved inside the project. For the future, when doing file
  106. operations in Godot, remember that "res://" is the resource path, and no
  107. matter the platform or install location, it is the way to locate where
  108. resource files are from inside the game.
  109. After saving the scene and pressing run scene again, the "Hello, World!"
  110. demo should finally execute:
  111. .. image:: /img/helloworld.png
  112. Success!
  113. .. _doc_scenes_and_nodes-configuring_the_project:
  114. Configuring the project
  115. -----------------------
  116. Ok, It's time to do some configuration to the project. Right now, the
  117. only way to run something is to execute the current scene. Projects,
  118. however, have several scenes so one of them must be set as the main
  119. scene. This scene is the one that will be loaded at the time the project
  120. is run.
  121. These settings are all stored in the engine.cfg file, which is a
  122. plaintext file in win.ini format, for easy editing. There are dozens of
  123. settings that can be changed in this file to alter how a project executes,
  124. so to make matters simpler, a project setting dialog exists, which is
  125. sort of a frontend to editing engine.cfg
  126. To access that dialog, simply go to Scene -> Project Settings.
  127. Once the window opens, the task will be to select a main scene. This can
  128. be done easily by changing the application/main_scene property and
  129. selecting 'hello.scn'.
  130. .. image:: /img/main_scene.png
  131. With this change, pressing the regular Play button (or F5) will run the
  132. project, no matter which scene is being edited.
  133. Going back to the project settings dialog. This dialog provides a lot
  134. of options that can be added to engine.cfg, and shows their default
  135. values. If the default value is ok, then there isn't any need to
  136. change it.
  137. When a value is changed, a tick is marked to the left of the name.
  138. This means that the property will be saved to the engine.cfg file and
  139. remembered.
  140. As a side note, for future reference and a little out of context (this
  141. is the first tutorial after all!), it is also possible to add custom
  142. configuration options and read them in run-time using the
  143. :ref:`Globals <class_Globals>` singleton.
  144. To be continued...
  145. ------------------
  146. This tutorial talks about "scenes and nodes", but so far there has been
  147. only *one* scene and *one* node! Don't worry, the next tutorial will
  148. deal with that...