Is it OK if one basic node is for the whole scene as a scene child?

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

I am a beginner at godot and I have a theory about making one basic node as the main scene and this node will not be replaced until it exits the game.

main_scene
     level_1

Inside that node I can store variables using get_parent().health= 25 then i can put variable in new level by

level_1

var = scene = level2.instance()
scene.health = get_parent().health
get_parent().add_child(scene)
free()

then

main_scene
     level_2

it can also load a new level without pausing the background music like this

main_scene
     level_2
     AudioStreamPlayer

Because main_scene is only a basic node which means that the child scene can be either 2d node or 3d node.
And if you want to save the game you can get all the necessary variables with get_parent().variable if the variable has been export

And this should be used for background loading while being cutscenes or load the most important scenes to load first so that the game will be more optimized, such as openworld games or games with large or scalable maps

I don’t know if this method works or not, I hope the community can answer

:bust_in_silhouette: Reply From: AlexTheRegent

Yes, this method will work. But there are also other ways to do it.
Godot already have one top level node called “root”. This node is persistent and all scenes are childs of this scene.change_scene method just replaces old scene with new. This root node also have autoloaded singletons as childs that are not replaced during scene changes. So you can store your variables in singleton, for example. This will make your code easier to maintain, because you will be able to access singleton from any script (no matter where this script in scene hierarchy) by singleton’s name (for example, Variables.health = 10). And you will be able to use change_scene method to switch scenes. So I recommend to use this approach rather than implementing your own scene switching algorithm.

Yes that is true.

But I’m still a little curious if, for example, the player exit the house_scene and load the city_scene but players forget to pick up items in the storage house so they need to go back into house_scene then have to load the whole house_scene again (I’ve been in that position and it was annoying when waiting for loading which only took something)

It can be fix if house_scene as a child in city_scene and use hide(). But this will cost performance.

If we replace root with a new scene, how do we want to keep the old scene for some time so that, for example, the player accesses the old scene without having to load it before the time runs out and free() is called (like saving the scene temporarily)

My game uses CPUParticle2D, Light2D and LightOccluder2D a lot.
So i don’t have confidence in leaving the game un-optimized just because it’s just a 2D game

echodoc | 2021-01-01 02:47

In case of caching scenes both approaches will work fine (just instance small scene inside big scene manually). But I think your approach is easier to maintain (since you really have to make own scene switching algorithm to make world seamless).
Nevertheless I’m still insisting on usage of singletons to store global variables. If your nodes will be deep in tree, you will have to call get_parent().get_parent() multiple times. And sometimes you will not know, how deep is your current node is. Moreover, if you reorder your nodes, you might change depth of nodes.

AlexTheRegent | 2021-01-01 09:18

Singletons are a good idea, maybe I’ll learn about them later.
I want to make main_scene like a server that takes care of changing scenes. In fact, I have made a script in main_scene to handle scene changes and enter the required variables automatically just by sending the scene paths and variables i want to enter and it will be more efficient if it use singletons too.
So i don’t get_parent() many time

echodoc | 2021-01-01 09:45