singletons_autoload.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. .. _doc_singletons_autoload:
  2. Singletons (AutoLoad)
  3. =====================
  4. Introduction
  5. ------------
  6. Godot's scene system, while powerful and flexible, has a drawback: there is no
  7. method for storing information (e.g. a player's score or inventory) that is
  8. needed by more than one scene.
  9. It's possible to address this with some workarounds, but they come with their
  10. own limitations:
  11. - You can use a "master" scene that loads and unloads other scenes as
  12. its children. However, this means you can no longer run those scenes
  13. individually and expect them to work correctly.
  14. - Information can be stored to disk in ``user://`` and then loaded by scenes
  15. that require it, but frequently saving and loading data is cumbersome and
  16. may be slow.
  17. The `Singleton Pattern <https://en.wikipedia.org/wiki/Singleton_pattern>`_ is a
  18. useful tool for solving the common use case where you need to store persistent
  19. information between scenes.
  20. Using this concept, you can create objects that:
  21. - Are always loaded, no matter which scene is currently running
  22. - Can store global variables, such as player information
  23. - Can handle switching scenes and between-scene transitions
  24. - Act like a singleton, since GDScript does not support global variables by design
  25. Autoloading nodes and scripts caters to this need.
  26. AutoLoad
  27. --------
  28. You can use AutoLoad to load a scene or a script that inherits from
  29. :ref:`Node <class_Node>`. Note: when autoloading a script, a Node will be
  30. created and the script will be attached to it. This node will be added to the
  31. root viewport before any other scenes are loaded.
  32. .. image:: img/singleton.png
  33. To autoload a scene or script, select ``Project -> Project Settings`` from the
  34. menu and switch to the "AutoLoad" tab.
  35. .. image:: img/autoload_tab.png
  36. Here you can add any number of scenes or scripts. Each entry in the list
  37. requires a name, which assigned as the node's ``name`` property.
  38. .. image:: img/autoload_example.png
  39. This means that any node can access a singleton named "PlayerVariables" with:
  40. .. tabs::
  41. .. code-tab:: gdscript GDScript
  42. var player_vars = get_node("/root/PlayerVariables")
  43. player_vars.health -= 10
  44. .. code-tab:: csharp
  45. var playerVariables = (PlayerVariables)GetNode("/root/PlayerVariables");
  46. playerVariables.Health -= 10; // Instance field.
  47. Or even simpler using the name directly:
  48. .. tabs::
  49. .. code-tab:: gdscript GDScript
  50. PlayerVariables.health -= 10
  51. .. code-tab:: csharp
  52. // Static members can be accessed by using the class name.
  53. PlayerVariables.Health -= 10;
  54. Note that autoload objects (scripts and/or scenes) are accessed just like any
  55. other node in the scene tree. In fact, if you look at the running scene tree,
  56. you'll see the autoloaded nodes appear:
  57. .. image:: img/autoload_runtime.png
  58. Custom scene switcher
  59. ---------------------
  60. This tutorial will demonstrate building a scene switcher using autoload. For
  61. basic scene switching, you can use the
  62. :ref:`SceneTree.change_scene() <class_SceneTree_change_scene>`
  63. method (see :ref:`doc_scene_tree` for details). However, if you need more
  64. complex behavior when changing scenes, this method provides more functionality.
  65. To begin, download the template from here:
  66. :download:`autoload.zip <files/autoload.zip>` and open it in Godot.
  67. The project contains two scenes: ``Scene1.tscn`` and ``Scene2.tscn``. Each
  68. scene contains a label displaying the scene name and a button with its
  69. ``pressed()`` signal connected. When you run the project, it starts in
  70. ``Scene1.tscn``. However, pressing the button does nothing.
  71. Global.gd
  72. ~~~~~~~~~
  73. Switch to the "Script" tab and create a new script called Global.gd. Make sure
  74. it inherits from ``Node``:
  75. .. image:: img/autoload_script.png
  76. The next step is to add this script to the autoLoad list. Open
  77. ``Project > Project Settings`` from the menu, switch to the "AutoLoad" tab and
  78. select the script by clicking the ``..`` button or typing its path:
  79. ``res://Global.gd``. Press "Add" to add it to the autoload list:
  80. .. image:: img/autoload_tutorial1.png
  81. Now whenever we run any scene in the project, this script will always be loaded.
  82. Returning to the script, it needs to fetch the current scene in the
  83. `_ready()` function. Both the current scene (the one with the button) and
  84. ``global.gd`` are children of root, but autoloaded nodes are always first. This
  85. means that the last child of root is always the loaded scene.
  86. .. tabs::
  87. .. code-tab:: gdscript GDScript
  88. extends Node
  89. var current_scene = null
  90. func _ready():
  91. var root = get_tree().get_root()
  92. current_scene = root.get_child(root.get_child_count() - 1)
  93. .. code-tab:: csharp
  94. using Godot;
  95. using System;
  96. public class Global : Godot.Node
  97. {
  98. public Node CurrentScene { get; set; }
  99. public override void _Ready()
  100. {
  101. Viewport root = GetTree().GetRoot();
  102. CurrentScene = root.GetChild(root.GetChildCount() - 1);
  103. }
  104. }
  105. Now we need a function for changing the scene. This function needs to free the
  106. current scene and replace it with the requested one.
  107. .. tabs::
  108. .. code-tab:: gdscript GDScript
  109. func goto_scene(path):
  110. # This function will usually be called from a signal callback,
  111. # or some other function in the current scene.
  112. # Deleting the current scene at this point is
  113. # a bad idea, because it may still be executing code.
  114. # This will result in a crash or unexpected behavior.
  115. # The solution is to defer the load to a later time, when
  116. # we can be sure that no code from the current scene is running:
  117. call_deferred("_deferred_goto_scene", path)
  118. func _deferred_goto_scene(path):
  119. # It is now safe to remove the current scene
  120. current_scene.free()
  121. # Load the new scene.
  122. var s = ResourceLoader.load(path)
  123. # Instance the new scene.
  124. current_scene = s.instance()
  125. # Add it to the active scene, as child of root.
  126. get_tree().get_root().add_child(current_scene)
  127. # Optionally, to make it compatible with the SceneTree.change_scene() API.
  128. get_tree().set_current_scene(current_scene)
  129. .. code-tab:: csharp
  130. public void GotoScene(string path)
  131. {
  132. // This function will usually be called from a signal callback,
  133. // or some other function from the current scene.
  134. // Deleting the current scene at this point is
  135. // a bad idea, because it may still be executing code.
  136. // This will result in a crash or unexpected behavior.
  137. // The solution is to defer the load to a later time, when
  138. // we can be sure that no code from the current scene is running:
  139. CallDeferred(nameof(DeferredGotoScene), path);
  140. }
  141. public void DeferredGotoScene(string path)
  142. {
  143. // It is now safe to remove the current scene
  144. CurrentScene.Free();
  145. // Load a new scene.
  146. var nextScene = (PackedScene)GD.Load(path);
  147. // Instance the new scene.
  148. CurrentScene = nextScene.Instance();
  149. // Add it to the active scene, as child of root.
  150. GetTree().GetRoot().AddChild(CurrentScene);
  151. // Optionally, to make it compatible with the SceneTree.change_scene() API.
  152. GetTree().SetCurrentScene(CurrentScene);
  153. }
  154. As mentioned in the comments above, we need to avoid the situation of deleting
  155. the current scene while it is still being used (i.e. its code is still running),
  156. so using :ref:`Object.call_deferred() <class_Object_call_deferred>`
  157. is required at this point. The result is that the second function will run
  158. at a later time when any code from the current scene has completed.
  159. Finally, we need to fill the empty callback functions in the two scenes:
  160. .. tabs::
  161. .. code-tab:: gdscript GDScript
  162. # Add to 'Scene1.gd'.
  163. func _on_Button_pressed():
  164. Global.goto_scene("res://Scene1.tscn")
  165. .. code-tab:: csharp
  166. // Add to 'Scene1.cs'.
  167. public void OnButtonPressed()
  168. {
  169. var global = (Global)GetNode("/root/Global");
  170. global.GotoScene("res://Scene2.tscn");
  171. }
  172. and
  173. .. tabs::
  174. .. code-tab:: gdscript GDScript
  175. # Add to 'Scene2.gd'.
  176. func _on_Button_pressed():
  177. Global.goto_scene("res://Scene2.tscn")
  178. .. code-tab:: csharp
  179. // Add to 'Scene2.cs'.
  180. public void OnButtonPressed()
  181. {
  182. var global = (Global)GetNode("/root/Global");
  183. global.GotoScene("res://Scene1.tscn");
  184. }
  185. Run the project and test that you can switch between scenes by pressing
  186. the button!
  187. Note: When scenes are small, the transition is instantaneous. However, if your
  188. scenes are more complex, they may take a noticeable amount of time to appear. To
  189. learn how to handle this, see the next tutorial: :ref:`doc_background_loading`