Overlaping scenes

Godot Version

Godot 4

Question

I´m currently having trouble with my scene_manager since it seems like it can´t delete the old scene so they just start overlaping heres the code of my scene manager the problem seems to be here

extends Node
@export var start_word_script: Node
@export var first_scene_index: int =0 ;
@export var editor_scenes:Array[PackedScene];
var is_dialogue_over:bool=false
var index: int = 0
var loadable_scenes : Array[Loadablescene]
var current_scene :LoadedScene
var scene_name :String
var sit_time: bool =false
func _ready() -> void:
	populate_loadable_scenes_list()
	load_level_scene_by_index(first_scene_index)
func _process(delta: float) -> void:
	
	return


func load_level_scene_by_index(index: int) ->LoadedScene:
	if (index< 0 or index>=loadable_scenes.size()):
		push_error()
		return null
	return load_level_scene(loadable_scenes[index], index)
func load_level_scene_by_name(scene_name:String) -> LoadedScene:
	var i: int=0
	while i < loadable_scenes.size():
		if scene_name == loadable_scenes[i].scene_name:
			return load_level_scene(loadable_scenes[i], i)
		i +=1
	push_error()
	return null

func load_level_scene(scene_to_load: Loadablescene, index:int) -> LoadedScene:
	if (not scene_to_load.scene.can_instantiate()):
		push_error()
		return null
	if (current_scene != null):
		current_scene.scene.queue_free()
		current_scene=null
	var new_scene : Node = scene_to_load.scene.instantiate()
	readjust_current_level_scenes_index
	if new_scene==null:
		push_error()
		return null
	current_scene=LoadedScene.new()
	
	current_scene.scene=new_scene
	current_scene.scene_index=index
	current_scene.scene_name=scene_to_load.scene_name
	
	add_child(new_scene)
	print(new_scene.name)
	return current_scene
class LoadedScene:
	var scene_index : int
	var scene_name :String
	var scene:Node

func populate_loadable_scenes_list() -> void:
	for scene in editor_scenes:
		print(add_level_scene_to_scene_array(scene));
		
func add_level_scene_to_scene_array(scene:PackedScene) -> int:
	if (scene==null):
		return -1
	
	var scene_name =scene.resource_path.get_file().get_basename()
	
	print(scene_name)
	
	var i : int=0;
	while i < loadable_scenes.size():
		if (scene_name==loadable_scenes[i].scene_name):
			printerr("duplicate scene name: "+ name)
			return -1
		i +=1
	var loadable_scene :Loadablescene = Loadablescene.new()
	
	loadable_scene.scene_name=scene_name
	loadable_scene.scene= scene
	loadable_scenes.append(loadable_scene)
	
	return loadable_scenes.size() -1
	
class Loadablescene:
	var scene_name:String
	var scene:PackedScene
	
	
	
func remove_level_scene_drom_array_by_index(index: int) -> bool :
	if (index< 0 or index>loadable_scenes.size()):
		return false
		
	loadable_scenes.remove_at(index)
	readjust_current_level_scenes_index()
	
	return true
	
func readjust_current_level_scenes_index() -> void:
	var i : int=0
	while i < loadable_scenes.size():
		if loadable_scenes[i].scene_name==current_scene.scene_name:
			current_scene.scene_index=i
			return
		i+=1
func remove_level_scene_drom_array_by_name(scene_name:String) -> bool:
	var i : int=0
	while i < loadable_scenes.size():
		if loadable_scenes[i].scene_name==scene_name:
			loadable_scenes.remove_at(i)
			readjust_current_level_scenes_index()
			return true
		i +=1
	return false

Are you setting your first scene as current? Maybe try printing current scene. I took a quick look at your code so may have missed it but I didn’t see the current scene being passed in before your queue free so maybe that’s my first thought.

I also think it would be safer to call deferred queue free of the scene to avoid any potential errors from freeing it while other processes are called.

Any runtime errors reported by the engine when swapping scenes?

queue_free() is already a deferred call. Calling it deferred wouldn’t make a difference.

when i try to print current scene this is the output

<RefCounted#-9223371950234007640>

it seems like load level scene by index returns error thanks too the push_error()

this is in load level scene by index wound this make it so the current scene is null before loading the next one?


if (current_scene != null):
	current_scene.scene.queue_free()
	current_scene=null

Well that’s your clue then. Which one?

My mistake, I’ve gotten errors in the past when calling queue free on a scene in regards calling methods on a null instance. Thanks for the info.

imagen_2026-09-18_183248063

this shows when i open it:

E 0:00:01:440 scene_tracker.gd:21 @ load_level_scene_by_index():
<C++ Source> core/variant/variant_utility.cpp:1024 @ push_error()
scene_tracker.gd:21 @ load_level_scene_by_index()
scene_tracker.gd:13 @ _ready()
scene_tracker.gd:51 @ load_level_scene()
scene_tracker.gd:23 @ load_level_scene_by_index()
scene_tracker.gd:13 @ _ready()

i think it pushes error thanks to this part of load lebel scene by index

if (index< 0 or index>=loadable_scenes.size()):
push_error()