Advertisement
Solidwave

Untitled

Aug 9th, 2023
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node
  2.  
  3. @onready var loading_scene = preload("res://Scenes/Load/load.tscn")
  4.  
  5. var load_value = 0
  6.  
  7. var current_scene = null
  8.  
  9. var scene_to_load = null
  10.  
  11. var load_callback : Callable = Callable()
  12.  
  13. var scene_to_load_instance  = null
  14.  
  15. var loading_thread : Thread = null
  16.  
  17. var finished : bool = false
  18.  
  19.  
  20. func _ready():
  21.     var root = get_tree().root
  22.     current_scene = root.get_child(root.get_child_count() - 1)
  23.    
  24. func _process(delta):
  25.     if scene_to_load != null && scene_to_load_instance == null:
  26.         print(ResourceLoader.load_threaded_get_status(scene_to_load))
  27.         match ResourceLoader.load_threaded_get_status(scene_to_load):
  28.             ResourceLoader.THREAD_LOAD_LOADED:
  29.                 var newScene = ResourceLoader.load_threaded_get(scene_to_load)
  30.                
  31.                 scene_to_load_instance = newScene.instantiate()
  32.                 loading_thread = Thread.new()
  33.                
  34.                 loading_thread.start(my_add_child)
  35.     if finished:
  36.         finished = false
  37.         load_value  = 0
  38.         scene_to_load = null
  39.        
  40.         if load_callback:
  41.             print(load_callback)
  42.             load_callback.call()
  43.         scene_to_load_instance = null
  44.  
  45.         if current_scene != null:
  46.             current_scene.free()
  47. #
  48. #
  49. ##      loading_thread.wait_to_finish()
  50.        
  51. #   if scene_to_load_instance:
  52. #       print(load_value)
  53.        
  54.  
  55. func load_scene(path : String):
  56.     # This function will usually be called from a signal callback,
  57.     # or some other function in the current scene.
  58.     # Deleting the current scene at this point is
  59.     # a bad idea, because it may still be executing code.
  60.     # This will result in a crash or unexpected behavior.
  61.  
  62.     # The solution is to defer the load to a later time, when
  63.     # we can be sure that no code from the current scene is running:
  64.  
  65.     call_deferred("deferred_load_scene", path)
  66.  
  67. func deferred_load_scene(path: String, loading: bool = false):
  68.     var root = get_tree().root
  69.    
  70.     current_scene = root.get_child(root.get_child_count() - 1)
  71.    
  72.     scene_to_load = path
  73.    
  74.     # It is now safe to remove the current scene
  75.     current_scene.free()
  76.  
  77.     # Load the new scene.
  78.     # Instance the new scene.
  79.     current_scene = loading_scene.instantiate()
  80.    
  81.     # Add it to the active scene, as child of root.
  82.     get_tree().root.add_child(current_scene)
  83.  
  84.     # Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
  85.     get_tree().current_scene = current_scene
  86.    
  87.     ResourceLoader.load_threaded_request(path)
  88.    
  89. func my_add_child():
  90. #   get_tree().root.call_deferred("add_child",scene_to_load_instance)
  91.     get_tree().root.add_child(scene_to_load_instance)
  92.    
  93.     finished = true
  94.    
  95. func _exit_tree():
  96.     loading_thread.wait_to_finish()
  97.    
  98.  
  99.    
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement