Using global autoloads to control scene transition

Godot Version

v4.7.2.stable.official

Question

I want to have a global autoload function which attaches and detaches scenes from nodes within a main tree that never changes. For example, there is a MainMenu node which can be moved around freely, and its child, a MainMenu root node which serves as an anchor for loaded scenes. This structure was suggested by some tutorials and I think it will be good for scalability, but I don’t understand the details. I want to use a script called DisplayManager to handle the loading and unloading of scenes within this framework. The problem is this: any script attached to the root node (“DisplayInsignio”) is not an autoload, so it won’t be accessible by other scripts, such as my DataManager, which needs to call functions to load and unload scenes. On the other hand, if I make DisplayManager into an autoload, it won’t know the location of scenes within the root tree, as it will no longer be attached to the root node.

Now, I am aware that I can probably solve this by using an autoload with hard-coded paths via get_tree() or something. This solution seems dangerous and inelegant though. For example, won’t it break autocompletes? If you think it’s the best/only solution, please tell me the safest way to handle it.

Overall, my question is: what is the most programmatic way to control scene transitions from an autoload, and to access the children of the root scene?

My scene tree looks like this. ManageDisplay is attached to DisplayInsignio. As you can see, Data, devFeed, and MainMenu have scenes attached by default. The “currentChapterSection” will contain the actual meat of the current gameplay mode, whether it’s a VN-style conversation, a crafting menu, or an action-adventure segment. Thus, it needs to be changed dynamically according to lists loaded from file. For example, Chapter 1 might include a conversation, then a battle, then a conversation.

ManageDisplay.gd looks like this:

extends Node3D
@onready var sectionRoot := $currentChapterSection/currentChapterSectionRoot

func getSectionRoot():
	return sectionRoot

I want ManageData to have a function like this:

func startSection():
	var sectionRoot = ManageDisplay.getSectionRoot()
    sectionRoot.add_child( blah blah blah... )

Autoloads are children of the scene tree root. They’ll be siblings of the top node in your main scene. Look at the remote scene tree at runtime.

You can use groups to fetch nodes without needing to know their paths.

Okay, thanks! I got a test version working with this code:

func getRoot():
	var root = get_tree().root
	return root.get_child(-1)

func getSectionRoot():
	var root = getRoot()
	var sectionRoot = root.get_node("currentChapterSection/currentChapterSectionRoot")
	return sectionRoot

func _ready() -> void:
	var sectionRoot = getSectionRoot()
	var packedScene = load("res://scenes/rts.tscn")
	var scene = packedScene.instantiate()
	sectionRoot.add_child(scene)
	

Also, you’re right that the more elegant code looks like this:

func getSectionRoot()->Node:
	var root = get_tree().get_nodes_in_group("SectionRoot")[0]
	return root

(With the currentChapterSectionRoot node being the only member of a global group called “SectionRoot”)

Yeah, I’d go with the groups. That way the autoload doesn’t need to deal with paths and things won’t break if you rearrange or rename nodes in your scene.