0 votes

I want to do the following:

I have a control node and I want to create an HBoxContainer and multiple (identical) TextureRects inside of the HBoxContainer.

The code I have for that is this:

func _ready():
    var ITEM = TextureRect.new()
    ITEM.texture = load("res://item.png")

    var row1 = HBoxContainer.new()
    for i in 5:
         row1.add_child(ITEM)

     add_child(row1)

This almost works, the problem is that Godot only attaches one child, not 5. Can someone help?

asked Mar 21 in Engine by 1izNoob (215 points)

1 Answer

0 votes
Best answer

You're trying to attach the same instance multiple times. Do this instead:

func _ready():
    var row1 = HBoxContainer.new()

    for i in 5:
        var ITEM = TextureRect.new()
        ITEM.texture = load("res://icon.png")
        row1.add_child(ITEM)

    add_child(row1)
answered Mar 21 by njamster (9,948 points)
selected Mar 21 by 1izNoob

ah thank you!

Just so that I understand: Once the instance is created I can only attach this one as a child once?

Correct. An instance is a unique realization of a scene. Much like one person cannot be in two different places at the same time. Even though it might look like that when you meet identical twins. ;)

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.