0 votes

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

Godot version 3.2
in Engine by (24 points)

1 Answer

+2 votes
Best answer

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.

by (1,312 points)
selected by

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

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.