Changing a scene is taking forever. How to fix?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ondesic

I am using C# and have been able to load a scene with ResourceLoader.LoadInteractive() in its own thread. This has been great.

However, once I get this huge scene loaded, when I use the get_tree().change_scene_to(newScene), the game freezes for a few seconds.

I know this is just adding newScene as a child. It is blocking the main thread. How do I change scenes asynchronous or without freezing the game?

:bust_in_silhouette: Reply From: CluelessCoder

idk if it works in godot 3 but you can try: get_tree().change_scene_to_file. Not sure if that will fix anything.

what I would recommend is adding a unique bootsplash that shows the player that the scene is loading in. I feel like in games its not easy to seamlessly move to a new scene especially if the scene is huge like you said. the engine needs time. But with a loading screen it would signal to the player whats going on instead of them thinking the games crashing.

I do have a splash screen with a progress bar. I can asynchronously load the scene into Godot and show the progress. The problem is, once it’s loaded into the memory, I need to add the scene as a child “add_child()” and THIS also is taking a few seconds. I need to know how to add a child asynchronously.

ondesic | 2023-05-05 19:18

are you loading the child in the scene but off screen or out of view? this might not be to much help but if you are you could add a visibilty enabler node. it will turn visibillty off when the node is off screen and it signifactly reduces the fps lag on your game. I recommened adding this to all your nodes and it will always work in the background helping reduce the lag. Godot has to constantly check what is on screen and it will freeze things up if theres to many.

I once created a cell simulation game that had cells reproduce exponentially. this caused massive freeze times when the childs spawned in but adding the visibilty enabler stopped the freeze.

CluelessCoder | 2023-05-08 01:13