0 votes

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.

in Engine by (12 points)

You should show your code.

extends Control

var a = 0
var x = 20

var cells_array = []

func generatematrix():
x = ($Dimension.text).toint();
cells
array.clear()

var c

var pos = Vector2(10, 20)


for i in range(x):

    cells_array.append([])

    c = LineEdit.new()

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

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][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 c.name (or cells_array[n][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.

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.

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.