Changing the value of a character in a word based on an array lookup

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

Imagine I have:

var hiddenWord = "BEAUTIFUL"

I also have an array:

var charactersPressed = ["A", "E", "I", "O", "U"]

I have another variable as such:

var displayWord = ""

I want to work through hiddenWord letter by letter and if I come across a letter that lives in charactersPressed I want to push that character into displayWord. If the letters don’t match I want to push an X into displayWord.

So with the above example, displayWord would look like "XEAUXIXUX"

I’ve started with this kind of logic (below), but it’s beating me and I feel I’m making it harder than I need to, hence asking here.

func showWord(word):
		displayWord = ""	#reset to nothing so we can rebuild it

		if not word.empty():	#if word is actually present
			var length = word.length()	#Might not be needed.
			for letter in hiddenWord:
				for arrayLetter in charactersPressed:
					if letter == arrayLetter:
						displayWord.[SOME_KINDOF_ID] = letter  #how do I do this?
					else:
						displayWord.[SOME_KINDOF_ID] = letter  #how do I do this?

		get_node("wordLabel").set_text(displayWord)

EDIT: I know that the above loops will not produce the desired result. They will create a VERY long displayWord. That’s my next challenge. For now the big things is changing the displayWord character in a specific spot which is the first part of the puzzle for me.

:bust_in_silhouette: Reply From: Robster

OK I’ve figured it out. It turns out Godot has a little array function called has which allows me to ask if array has character in it. If so, then do stuff, else do other stuff. Super cool.

Here’s the code for anyone for future reference.

func showWord(word):
		displayWord = ""

		if not word.empty(): 
			for i in range(word.length()):
				if charactersPressed.has(word[i]):
					displayWord += word[i]
				else:
					displayWord += "X"
			print(word)

		get_node("wordLabel").set_text(displayWord)