change_scenes_manually.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. .. _doc_change_scenes_manually:
  2. Change scenes manually
  3. ======================
  4. Sometimes it helps to have more control over how you swap scenes around.
  5. A :ref:`Viewport <class_Viewport>`'s child nodes will render to the image
  6. it generates. This holds true even for nodes outside of the "current"
  7. scene. Autoloads fall into this category, and also scenes which you
  8. instantiate and add to the tree at runtime:
  9. .. tabs::
  10. .. code-tab:: gdscript GDScript
  11. var simultaneous_scene = preload("res://levels/level2.tscn").instantiate()
  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().root.add_child(simultaneous_scene)
  16. .. code-tab:: csharp
  17. public Node simultaneousScene;
  18. public MyClass()
  19. {
  20. simultaneousScene = ResourceLoader.Load<PackedScene>("res://levels/level2.tscn").Instantiate();
  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().Root.AddChild(simultaneousScene);
  27. }
  28. To complete the cycle and swap out the new scene with the old one,
  29. you 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. **Delete the existing scene.**
  34. :ref:`SceneTree.change_scene_to_file() <class_SceneTree_method_change_scene_to_file>` and
  35. :ref:`SceneTree.change_scene_to_packed() <class_SceneTree_method_change_scene_to_packed>`
  36. will delete the current scene immediately. You can also delete the
  37. main scene. Assuming the root node's name is "Main", you 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 processing, physics processing, 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. **Hide the existing scene.** By changing the visibility or collision
  55. detection of the nodes, you can hide the entire node sub-tree from the
  56. player's perspective. Use
  57. :ref:`CanvasItem.hide() <class_CanvasItem_method_hide>` to hide a scene and
  58. :ref:`CanvasItem.show() <class_CanvasItem_method_show>` to show it again.
  59. - Memory still exists.
  60. - Pro: You can still access the data if needed.
  61. - Pro: There's no need to move any more nodes around to save data.
  62. - Con: More data is being kept in memory, which will be become a problem
  63. on memory-sensitive platforms like web or mobile.
  64. - Processing continues.
  65. - Pro: Data continues to receive processing updates, so the scene will
  66. keep any data within it that relies on delta time or frame data
  67. updated.
  68. - Pro: Nodes are still members of groups (since groups belong to the
  69. :ref:`SceneTree <class_SceneTree>`).
  70. - Con: The CPU's attention is now divided between both scenes. Too much
  71. load could result in low frame rates. You should be sure to test
  72. performance as you go to ensure the target platform can support the
  73. load from this approach.
  74. 3. **Remove the existing scene from the tree.** Assign a variable
  75. to the existing scene's root node. Then use
  76. :ref:`Node.remove_child(Node) <class_Node_method_remove_child>` to detach the entire
  77. scene from the tree. To attach it later, use
  78. :ref:`Node.add_child(Node) <class_Node_method_add_child>`.
  79. - Memory still exists (similar pros/cons as hiding it from view).
  80. - Processing stops (similar pros/cons as deleting it completely).
  81. - Pro: This variation of "hiding" it is much easier to show/hide. Rather
  82. than potentially keeping track of multiple changes to the scene, you
  83. only need to call the add/remove_child methods. This is similar to
  84. disabling game objects in other engines.
  85. - Con: Unlike with hiding it from view only, the data contained within
  86. the scene will become stale if it relies on delta time, input, groups,
  87. or other data that is derived from :ref:`SceneTree <class_SceneTree>`
  88. access.
  89. There are also cases where you may wish to have many scenes present at the same
  90. time, such as adding your own singleton at runtime, or preserving
  91. a scene's data between scene changes (adding the scene to the root node).
  92. .. tabs::
  93. .. code-tab:: gdscript GDScript
  94. get_tree().root.add_child(scene)
  95. .. code-tab:: csharp
  96. GetTree().Root.AddChild(scene);
  97. Another case may be displaying multiple scenes at the same time using
  98. :ref:`SubViewportContainers <class_SubViewportContainer>`. This is optimal for
  99. rendering different content in different parts of the screen (e.g. minimaps,
  100. split-screen multiplayer).
  101. Each option will have cases where it is best appropriate, so you must examine
  102. the effects of each approach, and determine what path best fits your unique
  103. situation.