Creating new objects

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

Hello! I’m struggling to understand what I’m doing wrong. I`ve written code that has to create LineEdit for several times but I get:
Can’t add child ‘00’ to ‘Main’, already has a parent ‘Main’.
scene/main/node.cpp:1165 @ add_child()

One LinEdit even is created and I can’t understand why it works in this way.

You should show your code.

kidscancode | 2019-11-09 00:55

extends Control

var a = 0
var x = 20

var cells_array =

func _generate_matrix():
x = ($Dimension.text).to_int();

var c

var pos = Vector2(10, 20)

for i in range(x):
	c =

for n in range(x):
	if a == x:
				a = 0
	while a < x:
			add_child(cells_array[n][comment1-a], true)
			cells_array[n][comment1-a].name = String(n) + String(a)
			a +=1

Sietrin | 2019-11-09 05:34

You create x times a Lineedit in the “i” loop but just overwrite the variable c which holds that instance on the next loop iteration thus throwing away the previous instance.

Instead put the line to create a lineedit just before the first time you actually use it. That would be before cells_array[n][comment2-a].append(c)

Why the conditional for setting a=0 ? Though it might work this looks dangerous and unnecessary to me. If I understand that correctly then you can just set unconditionally a=0 (without a==x) at that place.

Assign the name directly to (or cells_array[n][comment2-a]) before add_child(.... It probably also works your way but you avoid that implicitly a preliminary name has to be assigned just to be overwritten anyway in the next line.

wombatstampede | 2019-11-09 11:53

It did work. You saved my reputation, time, mind and etc. I have no words to thank you. And I still don`t understand what exactly went wrong but will try.

Sietrin | 2019-11-09 12:27