change_scenes_manually.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.
  57. - Memory still exists.
  58. - Pro: You can still access the data if needed.
  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 any data within it that relies on delta time or frame data
  65. updated.
  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. You should be sure to test
  70. performance as you go to ensure the target platform can support the
  71. load from this approach.
  72. 3. **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 hiding it from view).
  77. - Processing stops (similar pros/cons as 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, you
  80. only need to call the add/remove_child methods. This is similar to
  81. 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 you may wish to have many scenes present at the same
  87. time, such as adding your own singleton at runtime, or preserving
  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().root.add_child(scene)
  92. .. code-tab:: csharp
  93. GetTree().Root.AddChild(scene);
  94. Another case may be displaying multiple scenes at the same time using
  95. :ref:`SubViewportContainers <class_SubViewportContainer>`. This is optimal for
  96. rendering different content in different parts of the screen (e.g. minimaps,
  97. split-screen multiplayer).
  98. Each option will have cases where it is best appropriate, so you must examine
  99. the effects of each approach, and determine what path best fits your unique
  100. situation.