Custom Scene Switcher, Docs

Godot Version

4.4

Question

I'm new and learning by reading through the documentation. In the [Custom Scene Switcher](https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html#custom-scene-switcher) example posted to the Godot docs, scene_1.tscn and scene_2.tscn utilize a globally-defined autoload by the name of Global. Later both scenes refer to Global's function (Global.goto_scene) in order to process a scene change. Doesn't this break the portability of the scenes by referring to a hardcoded autoloader? I'd figure emitting a signal would be the more portable option.

Since it’s a global function, it means you can access it from anywhere in your code. And since it’s a singleton, only ONE instance of it exists. This is desired so if you need to change the scene, it only happens in one place, and all the code that wants to change the scene will call that one global instance, instead of having a different scene change code in every single scene copy and pasted.

Singletons are meant to be “hardcoded” so you can easily access them, and you can be sure only a single instance of it exists.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.