Understanding and Smoothing Resource Management of Godot 4

Godot Version

4.3

Question

Please correct me if my understanding is incorrect.

Suppose I have a tank scene which contains the MeshInstance with a Texture and several AudioStreamPlayer3D for each sound effects such as CannonFiring, TankMoving, etc. When this tank scene is instantiated and got add_child into the root node, all the aforementioned associated resources that I put into the editor will be loaded, yes?

Assuming there are multiple huge graphical and audio resources to load, this means whenever I add this tank scene into the game, the game might briefly stutter just a bit before all the associated unloaded resources could be loaded into the memory, yes?

From there, and also from @GDScript — Godot Engine (stable) documentation in English , which mentioned:

Unless it’s already referenced elsewhere (such as in another script or in the scene), the resource is loaded from disk on function call, which might cause a slight delay, especially when loading large scenes. To avoid unnecessary delays when loading something multiple times, either store the resource in a variable or use preload.

This means, if I would like the player to have a smooth non-stuttering gameplay, I could create a node specifically for loading all the required resources and store them all in variables right before the player reaches the next area of the game, yes?

However, when the player reaches that point, if there are really a lot to load, the game will still stutter to load all those resources, yes?

Normally, a game could show “Now Loading” screen in this case to ease the transition. But if I want no “Now Loading” screen, and want a more smooth transition, is it possible to do something like asynchronous loading in advance?

No matter what you do, add_child will cause a stutter if there is going to be a stutter. The best you can do is to minimize the stutter until the Godot devs get around to making add_child thread-safe.

preload will load the scene into memory before the game starts, but it will still stutter at add_child. Background loading in a thread will have the same result.

You don’t have to have a loading screen, as you can do all the loading in the background, but there is no getting around the fact that add_child will cause a stutter when the scene is added to the scene tree.

Thank you for this information, @soapspangledgames

If this is the case, then what I can do is to add_child in advance, sneaking it in somewhere and make it so these children are not yet active (perhaps pause them or hide them somewhere until they are relevant to the player).

But from my test, I tried creating 100 bullets every 0.1 second, and add them all to the root node and they all queue_free themselves after a few seconds, disable vsync and I got more than 100 fps, and I don’t feel any stuttering. The bullet scene only contains 3 nodes however.

I guess, for your case, it sounds like your scene could contain hundred or thousand nodes when you add it to the game?

It’s going to vary. I have many custom nodes that add to the scene tree with no issues at all, while I also have some nodes that take an extra frame or two to add to the scene tree.

If the framerate dropped by just 1 or 2, that is fine. The players won’t notice much. If we are talking a sudden huge noticeable stutter every time something is added to the game, then that will ruin the experience.