scenes_and_nodes.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 are dozens of them.
  20. Nodes
  21. -----
  22. But let's start with the basics. A node is a fundamental building block for
  23. creating a game. A node:
  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 the creation of more complex functions.
  35. Don't worry if this doesn't click yet. We will continue to explore this over
  36. the next few sections. The most important fact to remember for now is that
  37. nodes exist and can be arranged this way.
  38. Now that the concept of nodes has been defined, the next logical
  39. step is to explain what a scene is.
  40. Scenes
  41. ------
  42. .. image:: /img/scene_tree_example.png
  43. A scene is composed of a group of nodes organized hierarchically (in
  44. tree fashion). Furthermore, a scene:
  45. - always has only one root node.
  46. - can be saved to disk and loaded back.
  47. - can be *instanced* (more on that later).
  48. Running a game means running a scene. A project can contain several scenes,
  49. but for the game to start, one of them must be selected as the main scene.
  50. Basically, the Godot editor is a **scene editor**. It has plenty of tools for
  51. editing 2D and 3D scenes as well as user interfaces, but the editor is based on
  52. the concept of editing a scene and the nodes that compose it.
  53. Creating a new project
  54. ----------------------
  55. Let's make these abstract concepts more concrete with an example. Following a
  56. long tradition in tutorials, we'll start with a "Hello World" project.
  57. This will introduce us to using the editor.
  58. If you run the godot executable outside of a project, the Project Manager
  59. appears. This helps developers manage their projects.
  60. .. image:: /img/project_manager.png
  61. To create a new project, click the "New Project" option. Choose and create a
  62. path for the project and specify the project name "New Project":
  63. .. image:: /img/create_new_project.png
  64. Editor
  65. ------
  66. Once you've created the "New Project", then open it. This will open the Godot
  67. editor:
  68. .. image:: /img/empty_editor.png
  69. As mentioned before, making games in Godot feels like being in a
  70. kitchen, so let's open the refrigerator and add some fresh nodes to the
  71. project. We'll begin with a "Hello World!" message that we'll put on the
  72. screen.
  73. To do this, press the "New Node" button (which looks like a plus symbol):
  74. .. image:: /img/newnode_button.png
  75. This will open the Create Node dialog, showing the long list of nodes
  76. that can be created:
  77. .. image:: /img/node_classes.png
  78. From there, select the "Label" node first. Searching for it is probably
  79. the quickest way:
  80. .. image:: /img/node_search_label.png
  81. And finally, create the Label! A lot happens when Create is pressed:
  82. .. image:: /img/editor_with_label.png
  83. First of all, the scene changes to the 2D editor (because Label is a 2D Node
  84. type), and the Label appears, selected, at the top left corner of the viewport.
  85. The node appears in the scene tree editor (box in the top left
  86. corner), and the label properties appear in the Inspector (box on the
  87. right side).
  88. The next step will be to change the "Text" Property of the label, let's
  89. change it to "Hello, World!":
  90. .. image:: /img/hw.png
  91. Ok, everything's ready to run the scene! Press the PLAY SCENE Button on
  92. the top bar (or hit F6):
  93. .. image:: /img/playscene.png
  94. Aaaand... Oops.
  95. .. image:: /img/neversaved.png
  96. Scenes need to be saved to be run, so save the scene to something like
  97. hello.tscn in Scene -> Save:
  98. .. image:: /img/save_scene.png
  99. And here's when something funny happens. The file dialog is a special
  100. file dialog, and only allows you to save inside the project. The project
  101. root is "res://" which means "resource path". This means that files can
  102. only be saved inside the project. For the future, when doing file
  103. operations in Godot, remember that "res://" is the resource path, and no
  104. matter the platform or install location, it is the way to locate where
  105. resource files are from inside the game.
  106. After saving the scene and pressing run scene again, the "Hello, World!"
  107. demo should finally execute:
  108. .. image:: /img/helloworld.png
  109. Success!
  110. .. _doc_scenes_and_nodes-configuring_the_project:
  111. Configuring the project
  112. -----------------------
  113. Ok, it's time to configure the project. Right now, the only way to run
  114. something is to execute the current scene. Projects, however, may have several
  115. scenes, so one of them must be set as the main scene. This is the scene that
  116. will be loaded any time the project is run.
  117. These settings are all stored in a project.godot file, which is a plaintext
  118. file in win.ini format (for easy editing). There are dozens of settings that
  119. you can change in this file to alter how a project executes. To simplify this
  120. process, Godot provides a project settings dialog, which acts as a sort of
  121. frontend to editing a project.godot file.
  122. To access that dialog, select Scene -> Project Settings. Try it now.
  123. Once the window opens, let's select a main scene. Locate the
  124. application/main_scene property and click on it to select 'hello.tscn'.
  125. .. image:: /img/main_scene.png
  126. With this change, now when you press the regular Play button (or F5), this
  127. scene will run, no matter which scene is actively being edited.
  128. The project settings dialog provides a lot of options that can be saved to a
  129. project.godot file and shows their default values. If you change a value, a
  130. tick is marked to the left of its name. This means that the property will be
  131. saved to the project.godot file and remembered.
  132. As a side note, it is possible to add custom configuration options and read
  133. them at run-time using the :ref:`ProjectSettings <class_ProjectSettings>`
  134. singleton. This is not something you need to worry about now (this is the
  135. first tutorial after all!), but we mention it for future reference.
  136. To be continued...
  137. ------------------
  138. This tutorial talked about "scenes and nodes", but so far there has been
  139. only *one* scene and *one* node! Don't worry, the next tutorial will
  140. expand on that...