nodes_and_scenes.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. .. The goal of this page is to explain more than doc_key_concepts_overview about nodes and scenes,
  2. get the user to create their first concrete scene.
  3. .. _doc_nodes_and_scenes:
  4. Nodes and Scenes
  5. ================
  6. In :ref:`doc_key_concepts_overview`, we saw that a Godot game is a tree of
  7. scenes and that each scene is a tree of nodes. In this lesson, we explain a bit
  8. more about them. You will also create your first scene.
  9. Nodes
  10. -----
  11. **Nodes are the fundamental building blocks of your game**. They are like the
  12. ingredients in a recipe. There are dozens of kinds that can display an image,
  13. play a sound, represent a camera, and much more.
  14. .. image:: img/nodes_and_scenes_nodes.webp
  15. All nodes have the following characteristics:
  16. - A name.
  17. - Editable properties.
  18. - They receive callbacks to update every frame.
  19. - You can extend them with new properties and functions.
  20. - You can add them to another node as a child.
  21. The last characteristic is important. **Together, nodes form a tree**, which is a powerful
  22. feature to organize projects. Since different nodes have different functions,
  23. combining them produces more complex behavior. As we saw before, you can build a
  24. playable character the camera follows using a :ref:`CharacterBody2D <class_CharacterBody2D>`
  25. node, a :ref:`Sprite2D <class_Sprite2D>` node,
  26. a :ref:`Camera2D <class_Camera2D>` node, and a :ref:`CollisionShape2D <class_CollisionShape2D>` node.
  27. .. image:: img/nodes_and_scenes_character_nodes.webp
  28. Scenes
  29. ------
  30. When you organize nodes in a tree, like our character, we call this construct a
  31. scene. Once saved, scenes work like new node types in the editor, where you can
  32. add them as a child of an existing node. In that case, the instance of the scene
  33. appears as a single node with its internals hidden.
  34. Scenes allow you to structure your game's code however you want. You can
  35. **compose nodes** to create custom and complex node types, like a game character
  36. that runs and jumps, a life bar, a chest with which you can interact, and more.
  37. .. image:: img/nodes_and_scenes_3d_scene_example.png
  38. The Godot editor essentially is a **scene editor**. It has plenty of tools for
  39. editing 2D and 3D scenes, as well as user interfaces. A Godot project can
  40. contain as many of these scenes as you need. The engine only requires one as
  41. your application's **main scene**. This is the scene Godot will first load when
  42. you or a player runs the game.
  43. On top of acting like nodes, scenes have the following characteristics:
  44. 1. They always have one root node, like the "Player" in our example.
  45. 2. You can save them to your local drive and load them later.
  46. 3. You can create as many instances of a scene as you'd like. You could have
  47. five or ten characters in your game, created from your Character scene.
  48. Creating your first scene
  49. -------------------------
  50. Let's create our first scene with a single node. To do so, you will need to
  51. :ref:`create a new project <doc_creating_and_importing_projects>` first. After
  52. opening the project, you should see an empty editor.
  53. .. image:: img/nodes_and_scenes_01_empty_editor.webp
  54. In an empty scene, the Scene dock on the left shows several options to add a
  55. root node quickly. :button:`2D Scene` adds a :ref:`Node2D <class_Node2D>` node,
  56. :button:`3D Scene` adds a :ref:`Node3D <class_Node3D>` node,
  57. and :button:`User Interface` adds a :ref:`Control <class_Control>` node.
  58. These presets are here for convenience; they are not mandatory.
  59. :button:`Other Node` lets you select any node to be the root node.
  60. In an empty scene, :button:`Other Node` is equivalent to pressing the :button:`Add Child Node`
  61. button at the top-left of the Scene dock, which usually adds
  62. a new node as a child of the currently selected node.
  63. We're going to add a single :ref:`Label <class_Label>` node to our scene. Its function is to draw
  64. text on the screen.
  65. Press the :button:`Add Child Node` button or :button:`Other Node` to create a
  66. root node.
  67. .. image:: img/nodes_and_scenes_02_scene_dock.webp
  68. The :ui:`Create New Node` dialog opens, showing the long list of available nodes.
  69. .. image:: img/nodes_and_scenes_03_create_node_window.webp
  70. Select the Label node. You can type its name to filter down the list.
  71. .. image:: img/nodes_and_scenes_04_create_label_window.webp
  72. Click on the Label node to select it and click the :button:`Create` button at
  73. the bottom of the window.
  74. .. image:: img/nodes_and_scenes_05_editor_with_label.webp
  75. A lot happens when you add a scene's first node. The scene changes to the 2D
  76. workspace because Label is a 2D node type. The Label appears, selected, in the
  77. top-left corner of the viewport. The node appears in the Scene dock on the left,
  78. and the node's properties appear in the Inspector dock on the right.
  79. Changing a node's properties
  80. ----------------------------
  81. The next step is to change the Label's :inspector:`Text` property. Let's change
  82. it to "Hello World".
  83. Head to the Inspector dock on the right of the viewport. Click inside the field
  84. below the :inspector:`Text` property and type "Hello World".
  85. .. image:: img/nodes_and_scenes_06_label_text.webp
  86. You will see the text draw in the viewport as you type.
  87. .. seealso:: You can edit any property listed in the Inspector as we did with
  88. the Text. For a complete reference of the Inspector dock, see
  89. :ref:`doc_editor_inspector_dock`.
  90. You can move your Label node in the viewport by selecting the move tool in the
  91. toolbar.
  92. .. image:: img/nodes_and_scenes_07_move_tool.webp
  93. With the Label selected, click and drag anywhere in the viewport to
  94. move it to the center of the view delimited by the rectangle.
  95. .. image:: img/nodes_and_scenes_08_hello_world_text.webp
  96. Running the scene
  97. -----------------
  98. Everything's ready to run the scene! Press the :button:`Run Current Scene`
  99. button in the top-right of the screen or press :kbd:`F6` (:kbd:`Cmd + R` on
  100. macOS).
  101. .. image:: img/nodes_and_scenes_09_play_scene_button.webp
  102. A popup invites you to save the scene, which is required to run it. Click the
  103. :button:`Save` button in the file browser to save it as ``label.tscn``.
  104. .. image:: img/nodes_and_scenes_10_save_scene_as.webp
  105. .. note:: The :ui:`Save Scene As` dialog, like other file dialogs in the editor, only
  106. allows you to save files inside the project. The ``res://`` path at
  107. the top of the window represents the project's root directory and
  108. stands for "resource path". For more information about file paths in
  109. Godot, see :ref:`doc_filesystem`.
  110. The application should open in a new window and display the text "Hello World".
  111. .. image:: img/nodes_and_scenes_11_final_result.webp
  112. Close the window or press :kbd:`F8` (:kbd:`Cmd + .` on macOS) to quit the running scene.
  113. Setting the main scene
  114. ----------------------
  115. To run our test scene, we used the :button:`Run Current Scene` button. Another button
  116. next to it, :button:`Run Project`, allows you to set and run the project's
  117. **main scene**. You can also press :kbd:`F5` (:kbd:`Cmd + B` on macOS) to do so.
  118. .. image:: img/nodes_and_scenes_12_play_button.webp
  119. .. note:: Running the project's *main scene* is distinct from running the
  120. *current scene*. If you encounter unexpected behavior, check
  121. to ensure you are running the correct scene.
  122. A popup window appears and invites you to select the main scene.
  123. .. image:: img/nodes_and_scenes_13_main_scene_popup.webp
  124. Click the :button:`Select` button, and in the file dialog that appears, double
  125. click on ``label.tscn``.
  126. .. image:: img/nodes_and_scenes_14_select_main_scene.webp
  127. The demo should run again. Moving forward, every time you run the project, Godot
  128. will use this scene as a starting point.
  129. .. note:: The editor saves the main scene's path in a project.godot file in your
  130. project's directory. While you can edit this text file directly to
  131. change project settings, you can also use the :menu:`Project > Project Settings`
  132. window to do so. For more information, see :ref:`doc_project_settings`.
  133. In the next part, we will discuss another key concept in games and in Godot:
  134. creating instances of a scene.