scenes_and_nodes.rst 7.1 KB

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