r/godot•
Spuba

Main Scene vs Autoload for Transitions

Hello, I am working on scene transitions in my game, and I am wondering what architecture people prefer. I am considering:

A: Have a main scene that loads each scene and adds/removes children at transition time. Persistent data can exist in the main scene.

B: Use change_scene() to transition between scenes and keep persistent data in Autoload scripts.

Are there any advantages/disadvantages to these two approaches? Do you have a preferred method for your games?

Posted Nov 5, 2022, 7:51 PM UTC

95
lielay9Contributor•

Definitely A for more fine control. Autoloads aren't needed for persistent data but are better suited for functionality that needs to be available from everywhere at any time. Moving from level to level is often a one-off task handled by one or a few scripts, i.e neither the logic nor the functionality needs to be global. That said, sometimes they can be a helpful design shortcut if done in moderation. I guess the point is that it should be considered whether they are suitable for the situation.

3
TheDuriel•

change_scene is a trap that will only cause you grief. It does literally nothing but remove_child followed by add_child.

1†
Awfyboy•

If you want to create a metroidvania and, for example, you want enemies to respawn after a set interval, then A will give you a much easier workflow.

You can keep data in the Main Scene instead of needing to access the Autoload every time.

1
__FuriousOrange__•

Option A seems to be the consensus, and in my experience I agree. I typically register a global transition manager script that has methods like fade in, fade out etc. The manager is responsible for adding and removing scenes to/from the main node. Once you get into the realm of state preservation between transitions change_scene isn’t going to be enough. In what I’m working on at the moment the transition manager has the option to take the active scene and push it on to a stack in memory before transitioning in the new scene. This allows for a transition history that can then be popped back one by one. Useful for stuff like inventory and map screens.

1

I would say it mostly comes down to personal preference.

Aside from your personal preference, depending on the specifics of your project, one method might be slightly faster or easier to implement compared to the other. But usually it's just down to your personal choice. Try both methods in a couple projects to see which one you like more.

Also keep in mind you can change this at any time. As your project grows, you will have to refactor your project structure anyway. Imho, to plan ahead and foresee the perfect project structure and architecture, you either need to be a psychic able to look into the future or have the Godot development experience of a 30 year veteran.

So expect to refactor from time to time and make use of the refactoring phases as regular part of your development routine to evaluate your project structure.

1