Created
January 19, 2022 23:31
-
-
Save workhorsy/4202e24d713925d4be48bd709a1944b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Control | |
| var _thread : Thread = null | |
| var _to_load := [] | |
| var _to_load_mutex := Mutex.new() | |
| func start_async_loader() -> void: | |
| _thread = Thread.new() | |
| var err = _thread.start(self, "_loader_thread", 0, Thread.PRIORITY_LOW) | |
| assert(err == OK) | |
| func load_scene_async(path : String, cb : FuncRef) -> void: | |
| _to_load_mutex.lock() | |
| _to_load.append({"path" : path, "cb" : cb}) | |
| _to_load_mutex.unlock() | |
| func _loader_thread(_arg : int) -> void: | |
| OS.delay_msec(1000) | |
| print("Running .........") | |
| while true: | |
| if not _to_load.empty(): | |
| _to_load_mutex.lock() | |
| var data = _to_load.pop_front() | |
| _to_load_mutex.unlock() | |
| var path = data["path"] | |
| var cb = data["cb"] | |
| var scene = ResourceLoader.load(path) | |
| var instance = scene.instance() | |
| cb.call_deferred("call_func", instance) | |
| OS.delay_msec(10) | |
| func _ready() -> void: | |
| start_async_loader() | |
| load_scene_async("res://Test.tscn", funcref(self, "_on_scene_loaded")) | |
| func _on_scene_loaded(instance : Node) -> void: | |
| print("FXIME: Do something with the loaded scene instance here ...") | |
| print("Loaded the scene %s" % [instance.name]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment