scene_tree.rst 5.8 KB

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