0 votes

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.

in Engine by (810 points)
edited by

1 Answer

0 votes
Best answer

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)
by (810 points)
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.

Categories