Godot Version
4.7 Beta 2
Question
Ask your question here! Try to give as many details as possible.
Hello! I’m new to Godot and most programming in general outside of the basics of syntax, so sorry if I say things that don’t make sense. I have been working on making a game for a few weeks now, but have a bit of a problem. I have been using the below code to switch scenes (directly taken from the Godot documentation):
extends Node
var current_scene = null
func _ready():
var root = get_tree().root
# Using a negative index counts from the end, so this gets the last child node of `root`.
current_scene = root.get_child(-1)
func goto_scene(path):
# This function will usually be called from a signal callback,
# or some other function in the current scene.
# Deleting the current scene at this point is
# a bad idea, because it may still be executing code.
# This will result in a crash or unexpected behavior.
# The solution is to defer the load to a later time, when
# we can be sure that no code from the current scene is running:
_deferred_goto_scene.call_deferred(path)
func _deferred_goto_scene(path):
# It is now safe to remove the current scene.
current_scene.free()
# Load the new scene.
var s = ResourceLoader.load(path)
# Instance the new scene.
current_scene = s.instantiate()
# Add it to the active scene, as child of root.
get_tree().root.add_child(current_scene)
# Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
get_tree().current_scene = current_scene
but my problem is that it replaces the entire scene, while what I want is for it to replace the Node2D here:

I want a few things to always be in the scene tree unless I explicitly tell them to leave, so I was trying to get it so that I have a global script that I can call that will take two main inputs (the node/scene I want to replace and the scene I want to pull in to replace it) anywhere I want. My problem is I cannot actually find documentation about how to do what I want, probably because I don’t know what to actually look for. Whenever I find guides on how to do this they’re showing me how to replace the entire scene tree like in that documentation, but I do not want my GameController or its child nodes gone. It should be able to load everything the game uses other than autoloads inside of it but without them always being there and eating up memory. I don’t know how to achieve this and I cannot figure out what to even look up. Could anyone help me out and maybe show me if there’s documentation for how to do exactly this that I’m just not seeing? Thanks!
(And if anyone is wondering why I’m using 4.7, it’s because apparently there were issues with 2D Skeletons in 4.6 and my game will probably rely on 2D Skeletons since I want swap-able equipment without having to redo tons of animations over and over, that’s the only reason I’m on 4.7)