Instancing a tscn causes a stack overflow?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By psear

I have this

extends Node2D

var tscn

func _ready():
	tscn = load("res://Node2D.tscn")
	var array1 = []
	array1.append(tscn)
	var array2 = []
	var temp = array1[0].instance()
	add_child(temp)
	array2.append(temp)
	print(get_children())
	array2[0].queue_free()
	print(get_children())

I am going to have a bunch of tscn’s loaded into an array in another project, and I would like to instance them from the array, and free them from the array. It basically helps for the organisation of the project.

The issue is, the above code gives me a stack overflow on line
var temp = array1[0].instance()
What is it I’m doing wrong?

Your code works for me. I don’t get any errors messages.

Adam_S | 2020-11-10 20:42

Agreed - same here. A quick test of the important parts of your code work fine here. Are you sure the scene you’re loading/instancing doesn’t have some inherent problem? Does simply loading and instancing it (without the array stuff) cause a similar issue for you?

jgodfrey | 2020-11-10 20:54

So I’ve tested this…
when I made this to experiment I had just one tscn file, the root nodes, and I was also using that to load and instance. If I made it a separate node, it works as normal. Is this supposed to happen? It’s not really relevant to my particular project but it would appear a root node can’t instance its own tscn file.

psear | 2020-11-10 21:58

:bust_in_silhouette: Reply From: Thomas Karcher

Based on your last comment, it seems clear that you created an endless recursion: If you load and instance a node in the ready function, which itself loads and instances the same node in its ready function, which… well, I guess you get the point.