Hello everybody!

I am programming a game with my friends and we have run into an issue when it comes to restarting our game. I wanted to understand what was happening exactly, so I created a project with minimal code to reproduce the error I could not understand.

In a nutshell: I am creating a brand new instance of my map scene when restarting, but it seems to keep datas from the old instance of that scene.

Can someone put an eye on my code please? I find it very disturbing. Problem in bold in the outputs.

The code for the main scene, that has no child:

#main.gd

extends Node2D

var current_map

var current_map_scene = preload("res://map.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
	load_map(current_map_scene)

func load_map(map_scene):
	var map_instance = map_scene.instance()
	if current_map != null:
		remove_child(current_map)
		current_map.queue_free()
	current_map = map_instance
	add_child(current_map)
	current_map.connect("restart",self,"_on_restart")
	
func _on_restart():
	load_map(current_map_scene)

The code in my map scene. It only has a single sprite (godot texture) as a child.

#map.gd

extends Node2D

signal restart

var error_dict : Dictionary

# Called when the node enters the scene tree for the first time.
func _ready():
	if !error_dict.has(0):
		error_dict[0] = []
	for element in get_children():
		error_dict[0].push_back(element)
	print("new error dict:")
	print(error_dict)

func _process(delta):
	if Input.is_action_just_pressed("ui_accept"):
		emit_signal("restart")
		
	if Input.is_action_just_pressed("ui_down"):
		print("Consult error dict")
		print(error_dict)

Steps to reproduce:

  • Put the main scene as the main of the project
  • Run
  • Press "down", then "space", then "down".

The output:

OpenGL ES 3.0 Renderer: GeForce GTX 1050 Ti/PCIe/SSE2
new error dict:
{0:[[Sprite:1142]]}
Consult error dict
{0:[[Sprite:1142]]}
new error dict:
{0:[[**Sprite:1142**], [Sprite:1148]]}
Consult error dict
{0:[[**Deleted Object**], [Sprite:1148]]}

The problem is at lines 7 and 9 in the outputs: I should only have sprite of the new instance of map, 1148, but it seems the one of old instance, 1142, is still here as well. Then the freeing deletes it but it is still here as a deleted node.

3 years later