random text from list

I would love to have a list of words, or phrases, that i could pick from at random.
after doing some searching i have put together the following:

Variable nextword is =0 Do=Random(60) to variable randomnumber
The cursor/touch is on go Do=1 to variable nextword
Touch or Left mouse down

Variable nextword is=1 Read “option” + ToString(Variable(randomnumber)) from file “wordlist.txt” and store as text in ToString(Variable(randomtext)
Do=0 to variable nextword
However when i ToString the value of variable randomtext to a text object it will only show 0
My list is saved as wordlist.txt in the same folder as the project. and has 61 entries like :

what am i doing wrong, or is there an easier/better way to do this?
Essentially i want to have a custom list of words, be able to pick one at random and display in text object.
Bonus points if i can do this without repeating words. Any help is greatly appreciated.

To save in a string variable, GD ask for the variable name only (“ToString(Variable( ))” is not needed), so it shoul be:

Conditions: Variable nextword is = 1 Actions: Read "option" + ToString(Variable(randomnumber)) from file "wordlist.txt" and store as text in randomtext

But the problem could be that you read from “option” + random, with option in lowercase, but the file has keys “OptionN” with Option in uppercase :slight_smile:

for sure, thanks for your help! I have in GD the punctuation correct, i must have missed the capital O when just in the post ( i’m not sure how to post a clip of the project)

I tried your suggestions and still am not able to read from file? is my guess, because i can change file location and output is the same. I am hoping to export as html5 and compile for android in the end.

  • so for testing in GD i have my wordlist.txt file in the same location as the *.gdg project file
    -i tried testing after exporting as html5 and placing the wordlist.txt in the www file and that does not work either.
    Usually after this much searching i can solve my own problems, this one has me confused, but i wondering if it is the location of my file wordlist.txt, or possibly do i need to reference the exact location in GD when “reading” the file? Ex. : C:/User/GDprojects/wordgame/wordlist.txt?


I think i attached a screen shot

In the first event, in the first action, you set the text object string this way:

Do = ToString(Variable(randomtext)) to the text of wordtext

This is an error, because you read the variable through Variable(), but this function returns the numeric value of the variable, and as randomtext is a string Variable(randomtext) returns 0, then you convert this number to string through ToString() function, so ToString(Variable(randomtext)) returns “0”.
You have to use VariableString(), wich returns the string value of the variable:

Do = VariableString(randomtext) to the text of wordtext

Thanks again for your response, i have fixed the error you found, and i still get the same result.
0 in the text object.
I have made projects previously to be able to write and read successfully (xp, health, player level variable stuff) -
but have never been able to load from a pre-existing file like this. so i am curious if i have the file in the wrong location, or if i am trying to read it from the wrong location. however, i maybe going down the wrong rabbit hole. perhaps i should step away for a bit.

all is good.
started the project over based on what Lizard had posted previously: http://www.forum.compilgames.net/viewtopic.php?f=19&t=6747&p=51628&hilit=text+variable#p51628
dunno what i was doing wrong, but now that it is reading from the file, i can continue with the rest, and see if an error will occur again.
Thanks for the help!!

meh… actually, had to take a break, don’t have much time now either - but it appears that all the above works wonderfully for a native game, but not so much in one for html5.

so any ideas how to make this work with an html5 project?