background_loading.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. :article_outdated: True
  2. .. _doc_background_loading:
  3. Background loading
  4. ==================
  5. When switching the main scene of your game (e.g. going to a new
  6. level), you might want to show a loading screen with some indication
  7. that progress is being made. The main load method
  8. (``ResourceLoader::load`` or just ``load`` from GDScript) blocks your
  9. thread, making your game appear frozen and unresponsive while the resource is being loaded. This
  10. document discusses the alternative of using the ``ResourceInteractiveLoader`` class for smoother
  11. load screens.
  12. ResourceInteractiveLoader
  13. -------------------------
  14. The ``ResourceInteractiveLoader`` class allows you to load a resource in
  15. stages. Every time the method ``poll`` is called, a new stage is loaded,
  16. and control is returned to the caller. Each stage is generally a
  17. sub-resource that is loaded by the main resource. For example, if you're
  18. loading a scene that loads 10 images, each image will be one stage.
  19. Usage
  20. -----
  21. Usage is generally as follows
  22. Obtaining a ResourceInteractiveLoader
  23. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. .. code-block:: cpp
  25. Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(String p_path);
  26. This method will give you a ResourceInteractiveLoader that you will use
  27. to manage the load operation.
  28. Polling
  29. ~~~~~~~
  30. .. code-block:: cpp
  31. Error ResourceInteractiveLoader::poll();
  32. Use this method to advance the progress of the load. Each call to
  33. ``poll`` will load the next stage of your resource. Keep in mind that
  34. each stage is one entire "atomic" resource, such as an image, or a mesh,
  35. so it will take several frames to load.
  36. Returns ``OK`` on no errors, ``ERR_FILE_EOF`` when loading is finished.
  37. Any other return value means there was an error and loading has stopped.
  38. Load progress (optional)
  39. ~~~~~~~~~~~~~~~~~~~~~~~~
  40. To query the progress of the load, use the following methods:
  41. .. code-block:: cpp
  42. int ResourceInteractiveLoader::get_stage_count() const;
  43. int ResourceInteractiveLoader::get_stage() const;
  44. ``get_stage_count`` returns the total number of stages to load.
  45. ``get_stage`` returns the current stage being loaded.
  46. Forcing completion (optional)
  47. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. .. code-block:: cpp
  49. Error ResourceInteractiveLoader::wait();
  50. Use this method if you need to load the entire resource in the current
  51. frame, without any more steps.
  52. Obtaining the resource
  53. ~~~~~~~~~~~~~~~~~~~~~~
  54. .. code-block:: cpp
  55. Ref<Resource> ResourceInteractiveLoader::get_resource();
  56. If everything goes well, use this method to retrieve your loaded
  57. resource.
  58. Example
  59. -------
  60. This example demonstrates how to load a new scene. Consider it in the
  61. context of the :ref:`doc_singletons_autoload` example.
  62. First, we set up some variables and initialize the ``current_scene``
  63. with the main scene of the game:
  64. ::
  65. var loader
  66. var wait_frames
  67. var time_max = 100 # msec
  68. var current_scene
  69. func _ready():
  70. var root = get_tree().get_root()
  71. current_scene = root.get_child(root.get_child_count() -1)
  72. The function ``goto_scene`` is called from the game when the scene
  73. needs to be switched. It requests an interactive loader, and calls
  74. ``set_process(true)`` to start polling the loader in the ``_process``
  75. callback. It also starts a "loading" animation, which could show a
  76. progress bar or loading screen.
  77. ::
  78. func goto_scene(path): # Game requests to switch to this scene.
  79. loader = ResourceLoader.load_interactive(path)
  80. if loader == null: # Check for errors.
  81. show_error()
  82. return
  83. set_process(true)
  84. current_scene.queue_free() # Get rid of the old scene.
  85. # Start your "loading..." animation.
  86. get_node("animation").play("loading")
  87. wait_frames = 1
  88. ``_process`` is where the loader is polled. ``poll`` is called, and then
  89. we deal with the return value from that call. ``OK`` means keep polling,
  90. ``ERR_FILE_EOF`` means loading is done, anything else means there was an
  91. error. Also note we skip one frame (via ``wait_frames``, set on the
  92. ``goto_scene`` function) to allow the loading screen to show up.
  93. Note how we use ``OS.get_ticks_msec`` to control how long we block the
  94. thread. Some stages might load fast, which means we might be able
  95. to cram more than one call to ``poll`` in one frame; some might take way
  96. more than your value for ``time_max``, so keep in mind we won't have
  97. precise control over the timings.
  98. ::
  99. func _process(time):
  100. if loader == null:
  101. # no need to process anymore
  102. set_process(false)
  103. return
  104. # Wait for frames to let the "loading" animation show up.
  105. if wait_frames > 0:
  106. wait_frames -= 1
  107. return
  108. var t = OS.get_ticks_msec()
  109. # Use "time_max" to control for how long we block this thread.
  110. while OS.get_ticks_msec() < t + time_max:
  111. # Poll your loader.
  112. var err = loader.poll()
  113. if err == ERR_FILE_EOF: # Finished loading.
  114. var resource = loader.get_resource()
  115. loader = null
  116. set_new_scene(resource)
  117. break
  118. elif err == OK:
  119. update_progress()
  120. else: # Error during loading.
  121. show_error()
  122. loader = null
  123. break
  124. Some extra helper functions. ``update_progress`` updates a progress bar,
  125. or can also update a paused animation (the animation represents the
  126. entire load process from beginning to end). ``set_new_scene`` puts the
  127. newly loaded scene on the tree. Because it's a scene being loaded,
  128. ``instance()`` needs to be called on the resource obtained from the
  129. loader.
  130. ::
  131. func update_progress():
  132. var progress = float(loader.get_stage()) / loader.get_stage_count()
  133. # Update your progress bar?
  134. get_node("progress").set_progress(progress)
  135. # ...or update a progress animation?
  136. var length = get_node("animation").get_current_animation_length()
  137. # Call this on a paused animation. Use "true" as the second argument to
  138. # force the animation to update.
  139. get_node("animation").seek(progress * length, true)
  140. func set_new_scene(scene_resource):
  141. current_scene = scene_resource.instantiate()
  142. get_node("/root").add_child(current_scene)
  143. Using multiple threads
  144. ----------------------
  145. ResourceInteractiveLoader can be used from multiple threads. A couple of
  146. things to keep in mind if you attempt it:
  147. Use a semaphore
  148. ~~~~~~~~~~~~~~~
  149. While your thread waits for the main thread to request a new resource,
  150. use a ``Semaphore`` to sleep (instead of a busy loop or anything similar).
  151. Not blocking main thread during the polling
  152. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153. If you have a mutex to allow calls from the main thread to your loader
  154. class, don't lock the main thread while you call ``poll`` on your loader class. When a
  155. resource is done loading, it might require some resources from the
  156. low-level APIs (RenderingServer, etc), which might need to lock the main
  157. thread to acquire them. This might cause a deadlock if the main thread
  158. is waiting for your mutex while your thread is waiting to load a
  159. resource.
  160. Example class
  161. -------------
  162. You can find an example class for loading resources in threads here:
  163. :download:`resource_queue.gd <files/resource_queue.gd>`. Usage is as follows:
  164. ::
  165. func start()
  166. Call after you instance the class to start the thread.
  167. ::
  168. func queue_resource(path, p_in_front = false)
  169. Queue a resource. Use optional argument "p_in_front" to put it in
  170. front of the queue.
  171. ::
  172. func cancel_resource(path)
  173. Remove a resource from the queue, discarding any loading done.
  174. ::
  175. func is_ready(path)
  176. Returns ``true`` if a resource is fully loaded and ready to be retrieved.
  177. ::
  178. func get_progress(path)
  179. Get the progress of a resource. Returns -1 if there was an error (for example if the
  180. resource is not in the queue), or a number between 0.0 and 1.0 with the
  181. progress of the load. Use mostly for cosmetic purposes (updating
  182. progress bars, etc), use ``is_ready`` to find out if a resource is
  183. actually ready.
  184. ::
  185. func get_resource(path)
  186. Returns the fully loaded resource, or ``null`` on error. If the resource is
  187. not fully loaded (``is_ready`` returns ``false``), it will block your thread
  188. and finish the load. If the resource is not on the queue, it will call
  189. ``ResourceLoader::load`` to load it normally and return it.
  190. Example:
  191. ~~~~~~~~
  192. ::
  193. # Initialize.
  194. queue = preload("res://resource_queue.gd").new()
  195. queue.start()
  196. # Suppose your game starts with a 10 second cutscene, during which the user
  197. # can't interact with the game.
  198. # For that time, we know they won't use the pause menu, so we can queue it
  199. # to load during the cutscene:
  200. queue.queue_resource("res://pause_menu.tres")
  201. start_cutscene()
  202. # Later, when the user presses the pause button for the first time:
  203. pause_menu = queue.get_resource("res://pause_menu.tres").instantiate()
  204. pause_menu.show()
  205. # When you need a new scene:
  206. queue.queue_resource("res://level_1.tscn", true)
  207. # Use "true" as the second argument to put it at the front of the queue,
  208. # pausing the load of any other resource.
  209. # To check progress.
  210. if queue.is_ready("res://level_1.tscn"):
  211. show_new_level(queue.get_resource("res://level_1.tscn"))
  212. else:
  213. update_progress(queue.get_progress("res://level_1.tscn"))
  214. # When the user walks away from the trigger zone in your Metroidvania game:
  215. queue.cancel_resource("res://zone_2.tscn")
  216. **Note**: this code, in its current form, is not tested in real world
  217. scenarios. If you run into any issues, ask for help in one of
  218. `Godot's community channels <https://godotengine.org/community>`__.