background_loading.rst 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 retrieve 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 an 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. .. tabs::
  39. .. code-tab:: gdscript
  40. const ENEMY_SCENE_PATH : String = "Enemy.tscn"
  41. func _ready():
  42. ResourceLoader.load_threaded_request(ENEMY_SCENE_PATH)
  43. self.pressed.connect(_on_button_pressed)
  44. .. code-tab:: csharp
  45. using Godot;
  46. public partial class MyButton : Button
  47. {
  48. private const string EnemyScenePath = "Enemy.tscn";
  49. public override void _Ready()
  50. {
  51. ResourceLoader.LoadThreadedRequest(EnemyScenePath);
  52. Pressed += OnButtonPressed;
  53. }
  54. }
  55. Now ``_on_button_pressed`` will be called when the button is pressed.
  56. This method will be used to spawn an enemy.
  57. .. tabs::
  58. .. code-tab:: gdscript
  59. func _on_button_pressed(): # Button was pressed.
  60. # Obtain the resource now that we need it.
  61. var enemy_scene = ResourceLoader.load_threaded_get(ENEMY_SCENE_PATH)
  62. # Instantiate the enemy scene and add it to the current scene.
  63. var enemy = enemy_scene.instantiate()
  64. add_child(enemy)
  65. .. code-tab:: csharp
  66. private void OnButtonPressed() // Button was pressed.
  67. {
  68. // Obtain the resource now that we need it.
  69. var enemyScene = (PackedScene)ResourceLoader.LoadThreadedGet(EnemyScenePath);
  70. // Instantiate the enemy scene and add it to the current scene.
  71. var enemy = enemyScene.Instantiate();
  72. AddChild(enemy);
  73. }