scene changer not removing child

Godot Version

godot version 4.4.1 stable

Question

I’m working on a small 2d game, and currently I’m working with the scene changer. the idea of the code is, i can flag the two global variables from anywhere, and the scene changer script takes the data in the global variables and changes the scene. the reason im doing it from a node, and not using the root, is because i have a whole camera and shader setup that needs to be always loaded, and is set up so that as long as i have the scenes as child nodes, it works without any hiccups. my script is this.

extends Node2D
var instance

func _ready():
var scene = load(“res://assets/main/title.tscn”)
instance = scene.instantiate()
add_child(instance)
scene = null

func _physics_process(delta: float) → void:
if global.scene_change_check:
for child in self.get_children():
print(“clearing children”)
self.remove_child(child)

	var scene = load(global.scene_change)
	instance = scene.instantiate()
	add_child(instance)
	scene = null
	global.scene_change_check = false

for the most part, it works perfectly, but the only issue is that when it calls that it cleared the child, the current scene stays, and the one its changing to is just slapped over it.

Try using queue_free() instead of remove_child()

yeah, still same issue. tried queue_free(), free(), and remove_child(). if you wanted, i could send a copy of the whole program, so you can see for yourself.

Why isn’t the scene changer stored on the global so you can call a function when needed instead of checking every frame in _physics_process?

Can you explain what you expect to happen versus what really happens? The code looks fine other than strange process and not using queue_free. It also won’t delete any node that aren’t direct children.


Make sure to format scripts surrounded by three ticks ```