Simplifying variable assignment

Godot Version

4.2.2

Question

Is there any way to simplify these 10 lines of code into 1 loop?
I’m new to coding so sorry if this is easy fix.

$TextEdit.text = IndividualLetters[0]
	$TextEdit2.text = IndividualLetters[1]
	$TextEdit3.text = IndividualLetters[2]
	$TextEdit4.text = IndividualLetters[3]
	$TextEdit5.text = IndividualLetters[4]
	$TextEdit6.text = IndividualLetters[5]
	$TextEdit7.text = IndividualLetters[6]
	$TextEdit8.text = IndividualLetters[7]
	$TextEdit9.text = IndividualLetters[8]
	$TextEdit10.text = IndividualLetters[9]

I assume, that you have 10 child TextEdit nodes.
You can access child nodes by index with the function get_child()
So your code should loop on the index and inside the loop it could look something like

get_child(index).text = IndividualLetters[index]
1 Like

$ is a shortcut for get_node() so you can just simplify with

for i in range(1, 11):
	get_node("TextEdit" + str(i)).text = str(IndividualLetters[i])
2 Likes

after i put this it says 'invalid set index ‘text’ (on base: ‘null insatnce’ with value of type ‘string’)

Well, according to your code perhaps you need to rename the first TextEdit to TextEdit1, and the index of IndividualLetters is 1 smaller than index of TextEdit.

for i in range(10):
	get_node("TextEdit" + str(i + 1)).text = IndividualLetters[i]

I think the problem is the .text not working because when i type that there is no prompt for me to press tab and then it auto finishes which makes me feel like it isnt doing anything. Like i cant acces the property’s of the node

No that’s just autocomplete. The editor cannot guess what the type of get_node("TextEdit" + str(i + 1)) is, it’s normal, and it still has a text property. The problem is in naming it, your first TextEdit doesn’t have a number, but the script assumes it’s named TextEdit1. That’s why they recommend renaming it here:


The first suggestion should work fine too, let’s combine these two suggestions.

for index in range(10):
    get_child(index).text = IndividualLetters[index]

I think the problem was because the array, IndividualLetters, was empty/ null. Is my code here not working? Answer is a string and i was trying to put each bit of the array as each individual letter.

while digit < Answer.Length():
IndividualLetters[digit] = Answer[digit]
digit += 1

I don’t think IndividualLetters was empty

This says Godot couldn’t set “text” on a null instance to a string

null.text = "string" #error!

The problem being null, your IndividualLetters shows it was populated by some string. You could try printing it in full, see what print(IndividualLetters) gives you.

You are right, IndividualLetters is populated however when i run this new code the first 3 letters of it seem to be disregarded and the first TextEdit has the 4th letter of IndividualLetters and 2nd TextEdit has the 5th letter of IndividualLetters and so on.

Maybe your text edits aren’t in the right order? Could you show an example of what you mean? Maybe your scene tree as well?

The rectangles are the TextEdit’s and the big box is where the player put’s there word. next to it is a button you press after you’ve finsished your word. I then want to display the word into individual letters per TextEdit.

Awesome! I assume you are using the get_child method; the first three children of your “Background” node are the AnswerBox, DoneButton, and AnswerButton. It will set those texts first. I recommend putting all of your TextEdits as children of one new node, like so.

2024-08-21-133737_262x370_scrot

Now you can use this inner “Control” node’s children only.

for index in range(10):
    $Control.get_child(index).text = IndividualLetters[index]
1 Like

Thank you so much! I’m sure you could tell this is my first project without a tutorial. You explained everything so well.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.