Advertisement
irisgames404

loading screen

May 27th, 2023
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func load_scene(current_scene, next_scene):
  2.     # add loading scene to the root
  3.     var loading_scene_instance = loading_scene.instantiate()
  4.     get_tree().get_root().call_deferred("add_child",loading_scene_instance)
  5.    
  6.     # find the targeted scene
  7.     var loader = ResourceLoader.load_threaded_request(next_scene)
  8.    
  9.     #check for errors
  10.     if loader == null:
  11.         # handle your error
  12.         print("error occured while getting the scene")
  13.         return
  14.  
  15.     current_scene.queue_free()
  16.     # creating a little delay, that lets the loading screen to appear.
  17.     await get_tree().create_timer(0.5).timeout
  18.  
  19.     # loading the next_scene using poll() function
  20.     # since poll() function loads data in chunks thus we need to put that in loop
  21.     while true:
  22.         var error = loader.poll()
  23.         # when we get a chunk of data
  24.         if error == OK:
  25.             # update the progress bar according to amount of data loaded
  26.             var progress_bar = loading_scene_instance.get_node("ProgressBar")
  27.             progress_bar.value = float(loader.get_stage())/loader.get_stage_count() * 100
  28.         # when all the data have been loaded
  29.         elif error == ERR_FILE_EOF:
  30.             # creating scene instance from loaded data
  31.             var scene = loader.get_resource().instantiate()
  32.             # adding scene to the root
  33.             get_tree().get_root().call_deferred("add_child",scene)
  34.             # removing loading scene
  35.             loading_scene_instance.queue_free()
  36.             return
  37.         else:
  38.             # handle your error
  39.             print('error occurred while loading chunks of data')
  40.             return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement