+3 votes

So I have a list of "items" that I want to load into some label nodes. The label is instanced when you run a function. I want to be able to load in values into these instanced labels using set_text but I'm not sure how to reference it.

An example:

var info_node = infowhatever.instance()
add_child(info_node)

get_node(info_node).set_text(info_one_var)

How would I go about doing this or is there a more fundamental way of achieving this? Thanks.

in Engine by (151 points)

1 Answer

+1 vote
Best answer

You can just use the info_node reference that you set when you instanced the node.

info_node.text = "whatever text you want"

If you have multiple nodes being instanced, taking turns being the reference of the variable info_node, and you are "losing" the reference to them, consider giving them unique names when you instance them. Then you can reference them later using the names. For example:

var counter = 1
...
var info_node = InfoNode.instance()
info_node.name = "my_node_" + String(counter)
counter += 1

Now I can reference the node and set its text like this:

var node_to_change = get_node("/root/.../my_node_1")
node_to_change.text = "whatever text you want"

Alternatively, when you add the Label nodes to the scene, you can also add them into an array, or even as children of a simple Node which you are using just as a container for them. Then you can reference them using their position index.

Here's an example using an array:

 var label_array = []
...
# the Label node is added to the array during your creation code
label_array.append(InfoLabel.instance()]
...
# to reference it later using an index (assuming it's 0)
var node_to_change = label_array[0]
node_to_change.text = "whatever text you want"

And here's an example using a basic Node as a container (assume its name is "LabelContainer")

var label_container = get_node("/root/.../LabelContainer")
...
# the Label node is added as child during your creation code
label_container.add_child(InfoLabel.instance()]
...
# to reference it later using an index (assuming it's 0)
var node_to_change = label_container.get_children()[0]
node_to_change.text = "whatever text you want"
by (1,571 points)
edited by

Thanks that was extremely helpful. And of course, I can just set the property without using a function.. Thanks again.

I seem to have another problem with my particular nodes though. Basically the node I'm instancing is a HBoxContainer with labels inside it. How do I reference the node that would be a child of the node that I'm instancing? Thanks.

Hmm nevermind. infonode.getchild(0) works!

Yes, or you can do something like info_node.get_node("Label1").

Even better, more readable. Thanks!

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.
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 webmaster@godotengine.org with your username.