Scene Preloading stopped working

Godot Version

Godot 4.3 stable official

Question

Suddenly, for no apparent reason, one of my scenes cannot be preloaded.
This was working, and although none of the related code has changed, the scene will not load when using a preloaded packed scene.

This works:
get_tree().change_scene_to_file("res://scenes/ui/lobby.tscn")

But this fails:

  var lobby_scene: PackedScene = preload("res://scenes/ui/lobby.tscn")
  get_tree().change_scene_to_packed(lobby_scene)

Breakpoint inspection of the lobby_scene: PackedScene var shows that it exists, but all of its internal arrays are null. How can this happen, and what do I do to fix it?

I don’t want to surrender on pre-loading, and I’m so frustrated with what seems to be another godot bug I’m about to scrap my project and walk away.

I’m not super familiar with how things work in GDScript, but I can make an educated guess.
The first thing I can think of is that you don’t instantiate your scene, and that might behave weird with change_scene_to_packed.
Take a look here about how to instantiate your scene:

I would also highly recommend writing your own system for switching out the current scene, as change_scene_to_packed might not be the best solution in your case. (But that’s up to you to decide after some experimentation!)

According to the documentation, change_scene_to_packed does the instantiation itself.

For clarity, the above code is in my “mainmenu.gd” script (attached to the root node of the initial scene), trying to load and switch to the lobby_scene

When you try the get_tree().change_scene_to_packed(lobby_scene) the editor didn’t throw any error? Check the Error tab if any related error appears.

The app just stops. it doesn’t toggle over to the debugger like it sometimes will with issues. However looking at the output one sees:

E 0:00:03:0631 mainmenu.gd:21 @ _on_host_pressed(): Parameter “new_scene” is null.
<C++ Source> scene/main/scene_tree.cpp:1433 @ change_scene_to_packed()
mainmenu.gd:21 @ _on_host_pressed()

It thinks the “new_scene” is null. When I look at the variable in runtime, however, it exists as an actual “packed scene” … but all of its internal arrays are size 0

Try to add @onready at the start of the line where you preload the scene.

No change

That’s strange, the error says you’re passing a null parameter, but the debugger says otherwise. Could you share the project in github for futher testing?

I would bet you have a cyclical reference. Does your lobby preload something that then preloads the lobby? What happens if you try change_scene_to_file("res://scenes/ui/lobby.tscn")?

1 Like

Well I think you’re on to somehting @gertkeno

This does work. (Which is what initially made the failure so confusing to me)

The code above is in my main menu scene, and the lobby scene does indeed preload the main menu scene (because the user can move back to the main menu from there, I thought I should pre-load it). When I removed the preload of main menu within the lobby scene … things started working again.

THANK YOU.

… I guess exiting the lobby back to the main menu can’t use preloading?

The issue is preload does try to load the resource before the game is run at all. So if two resources preload each other A is loaded and tries to load B which tries to load A, but A is incomplete because it needs to load B and a recursive detection kicks in to stop all this from continuing forever.

I don’t think you want to preload most scenes since they require so many other resources. If you want to improve load times you can use threaded requests and gets

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.