scene_tree.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. .. _doc_scene_tree:
  2. Using SceneTree
  3. ===============
  4. Introduction
  5. ------------
  6. In previous tutorials, everything revolved around the concept of
  7. nodes. Scenes are collections of nodes. They become active once
  8. they enter the *scene tree*.
  9. MainLoop
  10. --------
  11. The way Godot works internally is as follows. There is the
  12. :ref:`OS <class_OS>` class,
  13. which is the only instance that runs at the beginning. Afterwards, all
  14. drivers, servers, scripting languages, scene system, etc are loaded.
  15. When initialization is complete, :ref:`OS <class_OS>` needs to be
  16. supplied a :ref:`MainLoop <class_MainLoop>`
  17. to run. Up to this point, all this is internals working (you can check
  18. main/main.cpp file in the source code if you are ever interested to
  19. see how this works internally).
  20. The user program, or game, starts in the MainLoop. This class has a few
  21. methods, for initialization, idle (frame-synchronized callback), fixed
  22. (physics-synchronized callback), and input. Again, this is low
  23. level and when making games in Godot, writing your own MainLoop seldom makes sense.
  24. SceneTree
  25. ---------
  26. One of the ways to explain how Godot works is that it's a high level
  27. game engine over a low level middleware.
  28. The scene system is the game engine, while the :ref:`OS <class_OS>`
  29. and servers are the low level API.
  30. The scene system provides its own main loop to OS,
  31. :ref:`SceneTree <class_SceneTree>`.
  32. This is automatically instanced and set when running a scene, no need
  33. to do any extra work.
  34. It's important to know that this class exists because it has a few
  35. important uses:
  36. - It contains the root :ref:`Viewport <class_Viewport>`, to which a
  37. scene is added as a child when it's first opened to become
  38. part of the *Scene Tree* (more on that next).
  39. - It contains information about the groups and has the means to call all
  40. nodes in a group or get a list of them.
  41. - It contains some global state functionality, such as setting pause
  42. mode or quitting the process.
  43. When a node is part of the Scene Tree, the
  44. :ref:`SceneTree <class_SceneTree>`
  45. singleton can be obtained by calling
  46. :ref:`Node.get_tree() <class_Node_method_get_tree>`.
  47. Root viewport
  48. -------------
  49. The root :ref:`Viewport <class_Viewport>`
  50. is always at the top of the scene. From a node, it can be obtained in
  51. two different ways:
  52. .. tabs::
  53. .. code-tab:: gdscript GDScript
  54. get_tree().get_root() # Access via scene main loop.
  55. get_node("/root") # Access via absolute path.
  56. .. code-tab:: csharp
  57. GetTree().GetRoot(); // Access via scene main loop.
  58. GetNode("/root"); // Access via absolute path.
  59. This node contains the main viewport. Anything that is a child of a
  60. :ref:`Viewport <class_Viewport>`
  61. is drawn inside of it by default, so it makes sense that the top of all
  62. nodes is always a node of this type otherwise nothing would be seen.
  63. While other viewports can be created in the scene (for split-screen
  64. effects and such), this one is the only one that is never created by the
  65. user. It's created automatically inside SceneTree.
  66. Scene tree
  67. ----------
  68. When a node is connected, directly or indirectly, to the root
  69. viewport, it becomes part of the *scene tree*.
  70. This means that as explained in previous tutorials, it will get the
  71. ``_enter_tree()`` and ``_ready()`` callbacks (as well as ``_exit_tree()``).
  72. .. image:: img/activescene.png
  73. When nodes enter the *Scene Tree*, they become active. They get access
  74. to everything they need to process, get input, display 2D and 3D visuals,
  75. receive and send notifications, play sounds, etc. When they are removed from the
  76. *scene tree*, they lose these abilities.
  77. Tree order
  78. ----------
  79. Most node operations in Godot, such as drawing 2D, processing, or getting
  80. notifications are done in tree order. This means that parents and
  81. siblings with a lower rank in the tree order will get notified before
  82. the current node.
  83. .. image:: img/toptobottom.png
  84. "Becoming active" by entering the *Scene Tree*
  85. ----------------------------------------------
  86. #. A scene is loaded from disk or created by scripting.
  87. #. The root node of that scene (only one root, remember?) is added as
  88. either a child of the "root" Viewport (from SceneTree), or to any
  89. child or grandchild of it.
  90. #. Every node of the newly added scene, will receive the "enter_tree"
  91. notification ( _enter_tree() callback in GDScript) in top-to-bottom
  92. order.
  93. #. An extra notification, "ready" ( _ready() callback in GDScript) is
  94. provided for convenience, when a node and all its children are
  95. inside the active scene.
  96. #. When a scene (or part of it) is removed, they receive the "exit
  97. scene" notification ( _exit_tree() callback in GDScript) in
  98. bottom-to-top order
  99. Changing current scene
  100. ----------------------
  101. After a scene is loaded, it is often desired to change this scene for
  102. another one. The simple way to do this is to use the
  103. :ref:`SceneTree.change_scene() <class_SceneTree_method_change_scene>`
  104. function:
  105. .. tabs::
  106. .. code-tab:: gdscript GDScript
  107. func _my_level_was_completed():
  108. get_tree().change_scene("res://levels/level2.tscn")
  109. .. code-tab:: csharp
  110. public void _MyLevelWasCompleted()
  111. {
  112. GetTree().ChangeScene("res://levels/level2.tscn");
  113. }
  114. Rather than using file paths, one can also use ready-made
  115. :ref:`PackedScene <class_PackedScene>` resources using the equivalent
  116. function
  117. :ref:`SceneTree.change_scene_to(PackedScene scene) <class_SceneTree_method_change_scene_to>`:
  118. .. tabs::
  119. .. code-tab:: gdscript GDScript
  120. var next_scene = preload("res://levels/level2.tscn")
  121. func _my_level_was_completed():
  122. get_tree().change_scene_to(next_scene)
  123. .. code-tab:: csharp
  124. public void _MyLevelWasCompleted()
  125. {
  126. var nextScene = (PackedScene)ResourceLoader.Load("res://levels/level2.tscn");
  127. GetTree().ChangeSceneTo(nextScene);
  128. }
  129. These are quick and useful ways to switch scenes but have the drawback
  130. that the game will stall until the new scene is loaded and running. At
  131. some point in the development of your game, it may be preferable to create proper loading
  132. screens with progress bar, animated indicators or thread (background)
  133. loading. This must be done manually using autoloads (see next chapter)
  134. and :ref:`doc_background_loading`.