I use a basic scene to load the next, and then transition to it. I use the ResourceLoader to load the next scene. I also have a label showing the progress in %s. The next scene is not that big, so the process goes really fast, which usually results in the last frame showing 40-50% then transitioning. I wanted to show 100%, so I added a 0.2sec wait time prior to transitioning.
However this made it so that roughly one every ten launches the packed scene was null and it crashed. If anyone can give some insight into what might cause this I would appreciate it. Maybe it has something to do with the cache method?
Here is the code:
extends Control
@onready var menu_scene: PackedScene
func _ready():
ResourceLoader.load_threaded_request(Global.start_menu)
func _process(delta):
var progress = []
ResourceLoader.load_threaded_get_status(Global.start_menu, progress)
$Label.text = str(snapped(progress[0]*100, 0.01)) + "%"
if progress[0] == 1:
await get_tree().create_timer(0.2).timeout
menu_scene = ResourceLoader.load_threaded_get(Global.start_menu)
get_tree().change_scene_to_packed(menu_scene)
You should not wait for a timer to timeout on _process. _process is called every frame and it’s meant for you to do stuff related to whatever has happened since the last frame.
Instead, I suggest you wait those 0.2 seconds before loading the new scene.
It may be something like this:
extends Control
var menu_scene: PackedScene
func _ready():
set_process(false)
func _process(delta):
var progress = []
var status = ResourceLoader.load_threaded_get_status(Global.start_menu, progress)
$Label.text = str(snapped(progress[0] * 100, 0.01)) + "%"
match status:
ResourceLoader.THREAD_LOAD_LOADED:
menu_scene = ResourceLoader.load_threaded_get(path)
set_process(false)
get_tree().change_scene_to_packed(menu_scene)
# TODO You may want to do something when there is an error.
func request_start_menu_load(): # This function should be called from another script to start the loading process.
# TODO You should ignore calls to this function when there is an ongoing load request.
await get_tree().create_timer(0.2).timeout
set_process(true)
ResourceLoader.load_threaded_request(Global.start_menu)
Thanks a lot! Timers messing with the process function does makes sense.
I implemented the solution a bit differently to suit my code more, but the principle remained the same. Thanks again!