(Solved) Check if player typed a word correctly (which is in a .txt file containing a word-list)?

Lets say I have a long list of words listed in a .txt file.
The player gets points by correctly typing any one of words found in the .txt file.

Is it possible for GDevelop to check if a word typed by the player exists in a .txt file? The txt fie contains hundreds of words.
If not, what would be the most efficient method to check if the typed word exists?
I know I could create a individual variable for each word but it’d probably take way too long. Is there a quicker way to do it using the word-list in the .txt file?

Could I somehow copy-paste the words as one big chunk and get Gdevelop to acknowledge each word as it’s own unique variable string…?

Hi,
Yes it can be done.
You have an action to take the text of a .txt file and put it in a text variable.

Then use the action “compare 2 numbers”. search for a word in a text variable with the StrFind function.
This function returns the position of the searched string in the main string.
If it returns -1, then the words isn’t found.

So, your event will be:

  • if the search function is equal to -1, the word doesn’t exist.
3 Likes

Hi Bluzima, i’s good news to hear it can be done. I haven’t used any of those terms before so I’ll experiment with them all. Nicely explained, I got a good idea now of what to try. Thank you.

1 Like

@Gorguruga, keep in mind that strings are case sensitive. You may need to use ToLowerCase() or ToUppercase() on both strings.

2 Likes

I agree StrFind is the way to go. Just a heads up. If you want only exact matches then each word would need a delimiter like space, newline or comma (I’m sure your list already does) But you would have to include them in your search. Make sure the list starts and ends with your separator.

Example list: ,horse,boat,trailer,

If you search for “trail” it would find trailer and return the position. You would instead want to search for “,trail,” (or whatever the separator is) If you are looking for exact matches.

10/27 Edit for clarity: in the previous example. If you searched for the word trail, it would find the trail in trailer and return the position of the first letter in trailer.

3 Likes

@MrMen Thanks for the info, that’ll come in handy.

@Keith_1357 Thanks for the info, very handy. Close to a working version now but it only recognizes the first word in list. It’s in newline format, example;

a
and
all
actual

So right now the only word it recognizes is “a”.
How would I search for exact matches beyond the first line or best to reformat all to commas?

This is how the Compare 2 Numbers condition looks at the moment
image

Here I think the explanation why it only recognizes “a”

the event you show is not right.
Variable(Spellword) is a number, and so since your spellword is “a”, converted to number it will be 0 since “a” is not a number.
On the other side of the equation, searching “a” in the dictionnary will return the location of the first “a” found, and this location is 0 since the dictionnary has its first character “a”.
That’s why your condition is true for “a”, since it become 0=0.

Your event should be :

  • StrFind(VariableString(Dictionary),VariableString(SpellWord))=/-1
    if that’s true, so the search is different than -1, it means the word exist, has been found in the dictionnary.
    if that’s false, so the search is equal to -1, the word is not in the dictionnary.

In order to include the delimiters suggested by @Keith_1357, and needed, here is what your event should be:

  • StrFind(VariableString(Dictionary),NewLine()+VariableString(SpellWord)+NewLine())=/-1

The delimiters will be new lines. So make sure that the first character of your dictionary is a new line, and the last one as well.

1 Like

Hey Gorguruga, I would use the “compare 2 numbers” condition. StrFind returns -1 if not found or the position if found. So, as Bluzima said I would use:

StrFind(NewLine()+VariableString(dictionary)+NewLine(),VariableString(spellword)) ≠ -1

Also, make sure the first and last character of dictionary has a newline otherwise it won’t match the first ir last word.

I remember another thread where they said some OSs use a different character than newline. I’m not sure if that will apply here. If there’s still a problem it might be that.

Edit: I just checked the forum and the issue was going to be fixed in the next version. There was just an update. So, I’m not sure if it was fixed in this or a prior version. Just something to look out for.

3 Likes

Hey @Bluzima and @Keith_1357 , thanks for the info once again guys. Apologies for this late reply. Just tried again and I still can’t seem to get it to work unfortunately. I tried both variations of your code on the StrFind.

If player presses the C key then it’s supposed to Check if the word exists and a sound is played if the word is in the dictionary. But doesn’t work. What happens is that a sound is played when one letter is picked up. Any letter. If there are 2 or 3 letters there’s no sound. So it doesn’t seem to be checking the words in the dictionary, it just plays a sound if the player picks up 1 letter and then presses C key. Any idea where I could be going wrong?

1 Like

Hi. If there’s a NewLine() between each word in your dictionary than you need to also add NewLine() before and after your search word.

I’m not at my PC. So, my code might not be exact but it would be something like yours but with 2 more NewLine()

StrFind(NewLine() + VariableString(Dictionary) + NewLine(), NewLine() + VariableString(SpellWord) +NewLine() ) ≠ -1

Edit: I’m not sure where your C key is being processed? Is the letter C being added into your spellWord? Just making sure it’s not or it will throw off the search.

2 Likes

I’ll give it a try soon and see if it works Keith, thanks again. Below a quick example of my dictionary txt file. At the very top is a blank line before the “a”. Is the formatting ok?

The C key is being processed as only a key stroke i think. It isn’t added to the Spellword variable. That only gets updated when the player physically collects a letter with the player character (not when they type on the keyboard).

image

1 Like

Hi,
Have you checked with the debugger the string of variable SpellWord?
Doing so you could be sure of what SpellWord is being searched in the dictionary and so you could see in what case it works and in what case it doesn’t work.
(during the run game, you can refresh the debugger to get the present variables values)

BTW, it’s normal the sound is played for one letter since each single letter is part of the dictionary : letter “a” is a word and in the dictionary. What should be figure out is what is the SpellWord that is being searched when it doesn’t work as planned. That’s why I suppose the use of debugger could help.

1 Like

Hi, thanks for info. I haven’t used the debugger before so I’ll give it a go tonight but this might be worth mentioning first… When the player collects the letter, the variable SpellWord is updated and it’s displayed on screen. So the player can always see the word being spelled. Problem is right now, if player collects J (or any letter) then presses the c-key to run the StrFind command, it reacts like the word is in the dictionary, it plays the sound sample and clears the variable.
[/quote]

It should not?

“J” alone is a letter and a word as well, isn’t it?

1 Like

I reduced the dictionary to only words which start with letter A. Just for testing. So it shouldn’t recognize the letter J.

I reduced the dictionary to only words which start with letter A. Just for testing. So it shouldn’t recognize the letter J.

Are you still using NewLine() ?

StrFind(NewLine()+VariableString(dictionary)+NewLine(),NewLine() +VariableString(spellword)+NewLine()) ≠ -1

“a” alone would match the “a” in “apple” but NewLine()+“a”+NewLine() wouldn’t match NewLine()+“apple”+NewLine() only NewLine()+“apple”+NewLine() would match.

1 Like

@Keith_1357 @Bluzima
Thanks for your help guys. It’s working now with NewLine().
Glad it’s solved :slight_smile:

2 Likes