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