How do i call a child node which is instanced, but the child node is in every instance different?

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

I want to call a child node of a instance ,its a label and for every instance different
Under every path is a instance
I want the data from the label


I tried that,but it doesn’t take the right data, it takes the data from the scene, not from the instance

:bust_in_silhouette: Reply From: Moreus

All depends what you want with your nodes. In parent you can make list/array and when your child is ready add them to parent list. Later you can call public metod from your children just using foreach loop in list.
in Godot use “Editor/Take Screenshot” for screenshot

I don’t really understand, my English is very bad, but I just want to get the text from the label thats in the instance which gets generated in the main scene. Could you maybe show me how I can code this

DG_Games | 2023-04-04 13:41

You can learn how to use signals

Moreus | 2023-04-04 14:04

:bust_in_silhouette: Reply From: godot_dev_

Just like @Moreus suggests, here is sample code to add all instances to an array when the parent is ready and then access the text via a get_text function given the child’s index position in the list (PS I don’t know how to add color scheme to code posted on the forum, sorry for the colorless code)

const CARD_BASE_TEXTURE_RESOURCE = preload("res://path/to/your/")
var cardList = []
func _ready():
    #iterature over each child of the parent
    for c in self.get_children():
       #only add card base texture nodes to the list
          cardList .append(c)

 func getText(childIx):
     #bounds check
     if childIx < 0 or childIx >= cardList .size():
        return null
     var dataLabel=cardList[childIx].get_node("Data")
     return dataLabel.text
     #note, if your CARD_BASE_TEXTURE_RESOURCE  script handles getting a reference to the data label node, say the member is called data, then you could just do 'return cardList[childIx].data' instead of dynamically looking up the node each time

So I wrote the code in the mainscenescript, but the cardList is empty, could you tell me what “iterature” is because in google I dont find a translation for it and how can i get that return value, with the “ChildIx”? In the beginning where you call get_children waht does that mean exactly? Thanks for your answer and sorry for so many questions im very new but i learn quick

DG_Games | 2023-04-04 16:37

  • cardList is empty at the begginning and is populated when the parent node becomes ready. It will stay empty if your parent node doesn’t have any child nodes that have the “res://path/to/your/” attached to them
  • Sorry, “iterature” is a typo, I meant to say “iterate” (it generally means go over many items)
  • get_children is the function that returns an array of all child nodes of the parent node. In the context of the code, that function and the for loop give you access to each child node, as a variable c, of the parent
  • to get the text of the 7th CardBaseTexture child (assuming you start counting from 0), you would simply use getText(7) inside you parent node’s script, or if you have a reference to your parent node’s script, let’s call it theParent, then theParent.getText(7) will return the text string value from the Data label node of your 7th CardBaseTexture child of the parent.
    -note that the parent node may have multiple children that aren’t of type CardBaseTexture, so that’s why the for loop in the _ready function checks the child node’s type before adding it to the cardList array

godot_dev_ | 2023-04-04 16:50

I found an easy alternative:
In the CardBaseScript:

func get_label_text() -> String:
	return $CardBaseTexture/Data.get_text()

In the GameSceneScript:

var Card1 = CardInstance.get_label_text()

But thank you very much

DG_Games | 2023-04-04 17:35