How to retain the names of instantiated nodes?

Godot Version

4.4

Question

How do you retain a name of a node when being instantiated at runtime? I am making a quiz game where the player will choose how many number of quizzes to be instantiated.

There is a Quiz Container node that I made that is empty. It will only be filled when the player provided the number of quizzes at runtime. To make this short, I need a reference to those instantiated nodes.

I did a quiz_number variable and it works. Example, I instantiate 5 Quiz Buttons. Quiz1 Button’s text has the quiz_number 1, Quiz2 Button’s text has the quiz_number 2 as I see it on the Remote tab in the scene tree. I also see the Quiz Button names in the scene tree as normal (QuizButton@…, something like this).

But the problem is, when I instantiate an additional 5 quiz buttons, the quiz_number variable inside a QuizButton is still correct but the QuizButton names are different. Why is that happening? Or is there any way to do this differently (Like using groups or something)?

Screenshot 1: The Quiz 1 button reference is encircled on blue ink. The selected quiz is pointed by the arrow.

Screenshot 2: The name of the QuizButton (encircled by blue ink) is now different as I add 4 more QuizButtons. However, the current selected quiz is the same.

add_child’s second parameter will try to keep the node’s name, though you cannot have a sibling node named the exact same as it’s siblings.

add_child(my_instance, true)
1 Like

I edited my question with screenshots. Please do look at it. Or is it still the same solution with your first reply? Thank you very much.

I think my reply stands. Though why do you need the name of the instantiated button to be legible? Your screenshots seem like the name should not matter, could you explain the big picture issue you’re having? Why do you think you need to retain the names of the instantiated scene?

1 Like

What I need to do is when I am on the main scene, I want to hide every children of a QuizContainer node and show only the current Quiz node selected while the player starts answering the quizzes. But is there another way to do this?

What I did back then is when I am on the Create Quiz scene, the questions and answers that is written right there will go to a different scene called Quiz Answering Scene. Now I got a problem that when I write a quiz, that written questions and answers do not go to the Quiz Answering scene because I am using preload and load instead of just hiding and showing them.

If the number variable for the buttons are correct, maybe you can just check the number instead of using the name.

1 Like

Yup. I am thinking of looping through the quiz instances and checking the number they have.

You could give the question instance a property “answered”. Loop over all question instances and do with true answered property what u like.

1 Like

Thank you for all of your replies. :smile:

I actually found another solution. I just have to get the index of those nodes, store that, then let the instantiated quiz be named from it.