scene_tree.rst 5.5 KB

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