change_scenes_manually.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. .. _doc_change_scenes_manually:
  2. Change scenes manually
  3. ======================
  4. Sometimes it helps to have more control over how one swaps scenes around.
  5. As mentioned above, a :ref:`Viewport <class_Viewport>`'s child nodes
  6. will render to the image it generates. This holds true even for nodes outside
  7. of the "current" scene. Autoloads fall into this category, but so do
  8. scenes which one instances and adds to the tree at runtime:
  9. .. tabs::
  10. .. code-tab:: gdscript GDScript
  11. var simultaneous_scene = preload("res://levels/level2.tscn").instance()
  12. func _add_a_scene_manually():
  13. # This is like autoloading the scene, only
  14. # it happens after already loading the main scene.
  15. get_tree().get_root().add_child(simultaneous_scene)
  16. .. code-tab:: csharp
  17. public PackedScene simultaneousScene;
  18. public MyClass()
  19. {
  20. simultaneousScene = (PackedScene)ResourceLoader.Load("res://levels/level2.tscn").instance();
  21. }
  22. public void _AddASceneManually()
  23. {
  24. // This is like autoloading the scene, only
  25. // it happens after already loading the main scene.
  26. GetTree().GetRoot().AddChild(simultaneousScene);
  27. }
  28. To complete the cycle and swap out the new scene with the old one,
  29. developers have a choice to make. Many strategies exist for removing a scene
  30. from view of the :ref:`Viewport <class_Viewport>`. The tradeoffs involve
  31. balancing operation speed and memory consumption as well as balancing data
  32. access and integrity.
  33. 1. **We can delete the existing scene.**
  34. :ref:`SceneTree.change_scene() <class_SceneTree_method_change_scene>` and
  35. :ref:`SceneTree.change_scene_to() <class_SceneTree_method_change_scene_to>`
  36. will delete the current scene immediately. Developers can also delete the
  37. main scene though. Assuming the root node's name is "Main", one could do
  38. ``get_node("/root/Main").free()`` to delete the whole scene.
  39. - Unloads memory.
  40. - Pro: RAM is no longer dragging the dead weight.
  41. - Con: Returning to that scene is now more expensive since it must be
  42. loaded back into memory again (takes time AND memory). Not a problem
  43. if returning soon is unnecessary.
  44. - Con: No longer have access to that scene's data. Not a problem if
  45. using that data soon is unnecessary.
  46. - Note: It can be useful to preserve the data in a soon-to-be-deleted
  47. scene by re-attaching one or more of its nodes to a different scene,
  48. or even directly to the :ref:`SceneTree <class_SceneTree>`.
  49. - Processing stops.
  50. - Pro: No nodes means no process, physics process, or input
  51. handling. The CPU is available to work on the new scene's contents.
  52. - Con: Those nodes' processing and input handling no longer operate.
  53. Not a problem if using the updated data is unnecessary.
  54. 2. **We can hide the existing scene.** By changing the visibility or collision
  55. detection of the nodes, we can hide the entire node sub-tree from the
  56. player's perspective.
  57. - Memory still exists.
  58. - Pro: One can still access the data if need be.
  59. - Pro: There's no need to move any more nodes around to save data.
  60. - Con: More data is being kept in memory which will be become a problem
  61. on memory-sensitive platforms like web or mobile.
  62. - Processing continues.
  63. - Pro: Data continues to receive processing updates, so the scene will
  64. keep updated any data within it that relies on delta time or frame
  65. data.
  66. - Pro: Nodes are still members of groups (since groups belong to the
  67. :ref:`SceneTree <class_SceneTree>`).
  68. - Con: The CPU's attention is now divided between both scenes. Too much
  69. load could result in low frame rates. One should be sure to test
  70. performance as they go to ensure the target platform can support the
  71. load they are giving it.
  72. 3. **We can remove the existing scene from the tree.** Assign a variable
  73. to the existing scene's root node. Then use
  74. :ref:`Node.remove_child(Node) <class_Node_method_remove_child>` to detach the entire
  75. scene from the tree.
  76. - Memory still exists (similar pros/cons as with hiding it from view).
  77. - Processing stops (similar pros/cons as with deleting it completely).
  78. - Pro: This variation of "hiding" it is much easier to show/hide. Rather
  79. than potentially keeping track of multiple changes to the scene, one
  80. must only call the one method add/remove_child pair of methods. It is
  81. similar to disabling game objects in other engines.
  82. - Con: Unlike with hiding it from view only, the data contained within
  83. the scene will become stale if it relies on delta time, input, groups,
  84. or other data that is derived from :ref:`SceneTree <class_SceneTree>`
  85. access.
  86. There are also cases where one may wish to have many scenes present at the same
  87. time. Perhaps one is adding their own singleton at runtime, or preserving a
  88. a scene's data between scene changes (adding the scene to the root node).
  89. .. tabs::
  90. .. code-tab:: gdscript GDScript
  91. get_tree().get_root().add_child(scene)
  92. .. code-tab:: csharp
  93. GetTree().GetRoot().AddChild(scene);
  94. Perhaps instead they wish to display multiple scenes at the same time using
  95. :ref:`ViewportContainers <class_ViewportContainer>`. This is optimal in
  96. cases where the intent is to render different content in different parts of the
  97. screen. Minimaps and split-screen multiplayer are good examples.
  98. Each option will have cases where it is best appropriate, so one must
  99. examine the effects of each and determine what path best fits
  100. their unique situation.