0 votes

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?

in Engine by (251 points)

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

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?

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.

1 Answer

0 votes

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.

by (1,696 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.