background_loading.rst 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. .. _doc_background_loading:
  2. Background loading
  3. ==================
  4. Commonly, games need to load resources asynchronously.
  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, or you may want to load additional resources
  8. during gameplay.
  9. The standard load method
  10. (:ref:`ResourceLoader.load <class_ResourceLoader_method_load>` or GDScript's simpler
  11. :ref:`load <class_@GDScript_method_load>`) blocks your
  12. thread, making your game appear unresponsive while the resource is being loaded.
  13. One way around this is using ``ResourceLoader`` to load resources asynchronously
  14. in background threads.
  15. Using ResourceLoader
  16. --------------------
  17. Generally, you queue requests to load resources for a path using
  18. :ref:`ResourceLoader.load_threaded_request <class_ResourceLoader_method_load_threaded_request>`,
  19. which will then be loaded in threads in the background.
  20. You can check the status with
  21. :ref:`ResourceLoader.load_threaded_get_status <class_ResourceLoader_method_load_threaded_get_status>`.
  22. Progress can be obtained by passing an array variable via progress which will return
  23. a one element array containing the percentage.
  24. Finally, you retrieved loaded resources by calling
  25. :ref:`ResourceLoader.load_threaded_get <class_ResourceLoader_method_load_threaded_get>`.
  26. Once you call ``load_threaded_get()``, either the resource finished loading in
  27. the background and will be returned instantly or the load will block at this point like
  28. ``load()`` would. If you want to guarantee this does not block,
  29. you either need to ensure there is enough time between requesting the load and
  30. retrieving the resource or you need to check the status manually.
  31. Example
  32. -------
  33. This example demonstrates how to load a scene in the background.
  34. We will have a button spawn a enemy when pressed.
  35. The enemy will be ``Enemy.tscn`` which we will load on ``_ready`` and instantiate when pressed.
  36. The path will be ``"Enemy.tscn"`` which is located at ``res://Enemy.tscn``.
  37. First, we will start a request to load the resource and connect the button:
  38. ::
  39. const ENEMY_SCENE_PATH : String = "Enemy.tscn"
  40. func _ready():
  41. ResourceLoader.load_threaded_request(ENEMY_SCENE_PATH)
  42. self.pressed.connect(_on_button_pressed)
  43. Now ``_on_button_pressed`` will be called when the button is pressed.
  44. This method will be used to spawn an enemy.
  45. ::
  46. func _on_button_pressed(): # Button was pressed
  47. # Obtain the resource now that we need it
  48. var enemy_scene = ResourceLoader.load_threaded_get(ENEMY_SCENE_PATH)
  49. # Instantiate the enemy scene and add it to the current scene
  50. var enemy = enemy_scene.instantiate()
  51. add_child(enemy)