Delay during initialization of bigger scene

Godot Version

v4.7.2.stable.mono.official [ed1daf0bf]

Question

I have a scene that controls main gameplay (it has player, enemies, handles loading/unloading levels, etc), and to hide that split second when things are being set up (like player being moved to proper position) I’m throwing loading screen at the player.

It generally works (meaning that short set up moment is not visible with the loading screen), but once that scene is entered, there is like a single frame of grey screen (something you would see when launching empty scene), and despite my greatest efforts I couldn’t get rid of it. None of the other scenes are affected (like main menu).

To hide the problem I’m placing loading screen as very first node in the game scene (afaik scenes load from top to bottom), and instantly enabling it to hide the grey screen, and other nodes have all the time they need to enter the tree. But as I said - it doesn’t work. Grey screen is still visible for that split second.

The scene tree looks like this:

  • Game
    • LoadingScreen
    • All remaining nodes

It happens even in exported release version, in case nodes load faster there.

It isn’t immediately obvious from your description when the grey frame appears. Is it before or after loading the scene?

In any case, you should be able to solve your issue by introducing a delay at the point where the problem occurs. If it is literally a single frame, you can try something like this in your code (from docs):

Using SceneTree’s process_frame signal for delay

var node = Node3D.new()
# Make a Callable and bind the arguments to the node's rotate() call.
var callable = node.rotate.bind(Vector3(1.0, 0.0, 0.0), 1.571)
# Connect the callable to the process_frame signal, so it gets called in the next process frame.
# CONNECT_ONE_SHOT makes sure it only gets called once instead of every frame.
get_tree().process_frame.connect(callable, CONNECT_ONE_SHOT)

This connects a specified callable to the Scene Tree’s process_frame signal once. Connect your function that loads the scene to this signal and you should be good to go.


Also, does the “All remaining nodes” in your scene tree include the scene that is loaded; or is the scene tree you described the contents of that loaded scene? You may need to reconfigure your loading system if the latter is the case.

Let me know how it goes.

Order of events:

  1. Scene is opened.
  2. Grey screen.
  3. Nodes start to enter the tree (things that are required for every level), loading screen appears.
  4. _Ready on owner fires.
  5. _Ready requests loading of specified Level.
  6. Level is loaded and added to the scene tree.
  7. Loading screen disappears, everything works fine.

The grey screen is the only anomaly here that I need to get rid of. Wouldn’t adding ProcessFrame increase the delay even more?

Hmm… I see. So you need the loading screen to work for the main scene, not the loaded levels?

Yes, you’re correct. I had a different interpretation of your description.

So am I correct when I say you’re trying to hide the “grey screen” (empty scene) that is shown when the game is first opened?

Also, it would be nice if we had access to:

  • A video or sequence of images showcasing the problem.
  • The code involved with loading the scene(s)
  • The node setup for relevant scenes/objects

I’m not sure I have a solution for you but I will have a look around.

I loading is indeed the bottleneck - use threaded loading while displaying something simple on the screen.

I have managed to come up with completely dynamic solution for transition between levels - player has no idea that levels are loaded and unloaded dynamically with no loading screen. I only use loading screen for the very first load, so player doesn’t see certain entities being teleported to their desired spots.

So am I correct when I say you’re trying to hide the “grey screen” (empty scene) that is shown when the game is first opened?

Yes, this is the issue. It’s not the end of the world, but it’s not very elegant.

More detailed flow:

  1. Scene change requested from menu.
  2. Grey screen.
  3. Loading screen appears.

Scene change is requested the following way:

GetTree().CallDeferred(SceneTree.MethodName.ChangeSceneToFile, _gamePath);

You might ask why not:

GetTree().ChangeSceneToFile(_gamePath);

The answer is - I don’t know, but when I do not defer it, it consistently crashes the game with no error in the editor. Error can be viewed if you launch engine with console:

ERROR: Parameter "get_viewport()" is null. 
ERROR: Condition "!is_inside_tree()" is true.

Both point to this exact line of code.

I think the crash might be related to transitions between menus (AnimationPlayer). Crash happens before animation fully finishes. If this is the suspect for the grey screen (CallDeferred is so long it even goes further to next scene, which doesn’t make much sense, but at this point I will believe everything), I will investigate, but I’ve written menu code like 2 months ago, so I need a moment to remember how it exactly works.

The very first line of code that is executed after game scene is opened is this in LoadingScreen.cs

public override void _EnterTree()
{
    Visible = true;
}

This isn’t really needed, but I do it anyway if I forget to set LoadingScreen as Visible in the editor. Currently it is set to Visible.

Edit

I’ve made an edit because at first I’ve thought I don’t play menu transition animation when entering game from menu, but I actually do. When you launch the game dozens of times you don’t even notice those details.

So I’m not correct, right? The grey screen appears when a scene change is requested – not when the game first opens.

If I were to guess, I’d say the deferred call to ChangeSceneToFile() is the culprit.

As I eluded to before, you might need to alter the way you load your levels. Using ChangeSceneToFile() is a quick way to jump between authored scenes, but it doesn’t allow you to manage the root manually. This means that you aren’t able to add the loading screen to the tree before instantiating, adding, and loading the new scene which, in combination with your deferred call, produces the grey frame.

From Godot Docs | Using SceneTree:

[ChangeSceneToFile() and ChangeSceneToPacked()] are quick and useful ways to switch scenes but have the drawback that the game will stall until the new scene is loaded and running. At some point in the development of your game, it may be preferable to create proper loading screens with progress bar, animated indicators or threaded (background) loading. This must be done manually using Singletons (Autoload) and Background loading.

You seem to already know this though given your use of a “dynamic solution”. Try to do something similar when your first load the level.

  1. Instantiate and show loading screen
  2. Wait for loading screen to appear (in case it is not instant)
  3. Instantiate and add level to existing tree
  4. Wait for level to finish loading
  5. Hide the loading screen

You will need to manage level loading with your own system instead of using ChangeSceneToFile().


Does this make sense?

I have checked the code and CallDeferred is not the source of the problem. I would have noticed it if I have described the flow even more precisely.

  1. Start button clicked in menu.
  2. Hide menu animation plays.
  3. Menu is removed from the owning Node.
  4. Here scene change is called. It crashes without deferring because you can’t change scenes when node is removed at the same time, which we are doing in step 3.
  5. Scene is changed to game scene.
  6. Grey screen.
  7. Loading screen.

I think not relying on ChangeSceneToFile is the answer, but at this point it’s not feasible - I would have to rework too much. So grey screen will stay.

You need to show the loading screen before loading the “game scene”. You can’t expect the loading screen to hide something that exists before itself.

Is there a reason you are adding the loading screen last?

Loading screen is the first thing that appears after entering Game. It exists in Game scene. Menu scene has no idea it exists. Wouldn’t help much if it did though, because grey screen is result of transition between Menu and Game scene. It’s not visible on small scenes, for example going from Game to Menu is completely fine.

Game itself isn’t that demanding, everything that can be heavy is in Level, which is loaded dynamically by Game. Level is loaded after loading screen has been shown.

To summarize - I didn’t know that ChangeSceneToFile is a trap, but on the other hand it needs to exist because in some scenarios dynamic loading would be massive overkill.

ChangeSceneToFile() is not a “trap”. It’s a simple function for simple use cases.

You keep throwing this “dynamic loading” term around. I don’t really know what it means. You seem to be stuck on the idea that if you don’t use ChangeSceneToFile(), you have to create a super complex loading system to fix a tiny issue.

The steps I outlined previously are simple and don’t require much work. Maybe I haven’t explained it to a sufficient degree.

The loading screen should not be part of either scene (“Game”, “Menu”, or “Level”). It should be a sibling of the other scenes, and this is what prompts the need for a custom loading system.


I can give you a simple example tomorrow, if you like. It’s less complicated than you might think.

I guess I should just explain complete flow.

  1. Player clicks start/continue in Menu.
  2. Current menu (there are many submenus that are loaded/unloaded as needed) plays animation to hide.
  3. Once animation finishes, scene change is requested. Menu and Game are two separate scenes, they don’t exist at the same time.
  4. Game scene is entered. Now the “grey screen anomaly” can be seen for around single frame.
  5. Loading screen is the first child of Game, and it’s immediately displayed.
  6. Some basic set up happens like initializing game state, filling object pools, etc.
  7. Game requests Level to be loaded.
  8. Level is loaded using ResourceLoader, instantiated, and added as child of the Game node.
  9. Level informs Game that it’s own set up has been complete, and loading screen is hidden.

Loading screen works in a way that the set up is hidden (for example without it you can see player at x0 y0 for single frame), but it fails to hide that moment where Menu is thrown away and Game takes over (the grey screen).

Creating single scene that exists always and goes between Menu and Game is definitely not impossible, the thing is I’m not sure if it’s feasible in this scenario. The game I’m developing is around 90% complete. And it’s a lot of effort for single grey frame. Unless it’s not that much effort, and I’m missing something.

I have an idea how to implement it in non-invasive way. Perhaps there is cleaner way to do it if you design everything around it from the start, but it won’t be that bad either.

Menu and Game remains almost completely unchanged. New scene is introduced that exists at all times, and loads/unloads things on demand with zero ChangeSceneToFile usage. The only problem is how to get rid of the current ChangeSceneToFile. If I haven’t missed something, solution should be simple - global event bus (which I already have). The global scene can just listen for Game/Menu requesting scene change and react accordingly, and I have full and complete control over scene flow. No grey screen or other surprises.