Godot Version
v4.6.2
Question
Hello Godot people,
I have been trying to figure out how to check a string for the letters so that I can create objects based on those letters. For example I am making a book that has secret letters that represent real letters, eg - A = (a drawing of a square or something).
The plan is to have the string recorded in code and player can input the answer into a text box.
I want to be able to simply change the string in the code and the symbols in the game change to the relevant secret letters.
So I figured I need to identify the letters in the string and their placement, store that in a array or dict and set the positions relevent to my artwork. Then I will instantiate the letters which are all individual animatedsprite2d nodes.
I hope that makes sense. The problem I am facing is I can’t figure out how to make the code check for multiple letters.
I have a string to test that is just “Hello”.
Using a for loop I put each letter into an array. As below.
var stringTest = "Hello."
var lettersDict = []
func testString():
for x in stringTest:
lettersDict.append(x)
I tried using find and the main issue is when looking for a letter that comes up more than once like the “l”. If I do find it come up with a 2 which is correct for the first L. But I can’t work out how to do a simple bit of code to check again for the 2nd one.
So I thought if I run the whole alphabet against the lettersDict (which is actually an array), I could see how many times a letter is in there, but using a find it just tells me the first letter it finds not the second like with the “l” mentioned above.
var alphabetArray = ["a","b","c","d","e","f","g","h","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
for y in alphabetArray:
var index = lettersDict.find(y,0)
I’ve been going back and forth with putting the letters into an array, with using count and find but the problem is I can’t work out a simple way to make sure it repeatedly checks each letter except for doing multiple IF statements.
I think I am missing something simple and probably had a better solution before I got to this point but now I am lost!
I tried some If statements where I increase a variable called Index by +1 (which I use as the from in find) if there is more than 1 entry of the letter which kind of works but it just has gotten so messy.
if index >= 0:
var index2
index2 = lettersDict.find(y,index+1)
print("more than 1. ", index2)
if index2 >= 0:
var index3
index3 = lettersDict.find(y,index+2)
print("more than 2. ", index3)
Any help would be much appreicated!!
Thanks



