#Loading Screen Only Works Once?

15 messages · Page 1 of 1 (latest)

sharp fiber
#

This is my loading screen code.



func _ready():
    ResourceLoader.load_threaded_request(Brain.next_scene)

func _process(delta):
    var progress = []
    ResourceLoader.load_threaded_get_status(Brain.next_scene, progress)
    %Progress.text = str(progress[0]*100) + "%"
    
    if progress[0] == 1:
        var packed_scene = ResourceLoader.load_threaded_get(Brain.next_scene)
        get_tree().change_scene_to_packed(packed_scene)```
I made it to load the main game from the main menu, and it does that fine. Whenever a node wants to load, it'll call this simple function in my data management autoload script called `Brain`:
```func loadscene(scene: String):
    next_scene = scene
    get_tree().change_scene_to_packed(loadingscreen)```
Which will bring up the loading screen and run the code from the beginning. However, I've just added a button to go back to the main menu from the main game scene... and it's not working, it gets stuck on 0. 

I'm assuming this has something to do with the code assuming that whatever it's trying to run needs to be loaded from nothing, and it can't handle bringing up scenes that have already been used once before. How best should I change it to enable returning to the main menu?

(The main menu is simple 2D and the main game screen is a 3D scene that takes about 2 seconds to load, if that helps any. If I should be doing any 'unloading' in these processes, I'd like to know about that, too...)
leaden patio
#

Do you create a new instance of this loading screen every time you try to use it?

#

You also never update the progress variable

sharp fiber
heady mortar
#

_ready only runs once (on object insertion into scene), which is the only call to load_threaded_request I see in your script.

#

when get_status (eventually) returns that it's loaded, you have limited time to use the resulting object (I think it cleans up with call_deferred?)

#

so the first time through, it works perfectly -- ready fires, it loads, it's loaded, you go

#

the second time through, nothing calls _ready, and any previously fetched asset is gone (the request is long over)

#

easy fix, trash this scene when done & and make a new one every time. More complex fix, make it more reusable by fixing the above issues.

sharp fiber
#

I've managed to get it to a spot where it's called like this...

    next_scene = scene
    var lsi = loadingscreen.instantiate()
    get_tree().get_root().call_deferred("add_child", lsi)
    lsi.startload()```
And operates like this...
```func startload():
    ResourceLoader.load_threaded_request(Brain.next_scene)

func _process(delta):
    var progress = []
    ResourceLoader.load_threaded_get_status(Brain.next_scene, progress)
    %Progress.text = str(progress[0]*100) + "%"
    
    if progress[0] == 1:
        var packed_scene = ResourceLoader.load_threaded_get(Brain.next_scene)
        get_tree().change_scene_to_packed(packed_scene)
        queue_free()```
Which gets me there without needing to invoke 'ready'. But I'm not quite sure how to unload the original main menu screen, and so I'm still stuck at the beginning forever when I try to go back... hmm
heady mortar
#

actually, this version -- because of loadingscreen.instantiate() -- will call ready appropriately, so that's ok

#

as for unloading -- that's queue_free

#

(which it looks like you're calling in process when the progress is complete)

#

so what are you seeing? That looks to me like it unloads.

sharp fiber
#

Working on this a little, still not getting anywhere... I'm pretty sure it's cuz that queue_free() is only nixing the loading screen itself, and not the screen I came from. But no matter how I try to remember the previous scene and kill it neatly, it always causes some kind of crash...