Text file:
Code:
{
"list" : [ "word1", "word2", "word3", "word4" ]
}
GameMaker code:
Code:
wordList = -1;
var fname = "nameOfYourFile.txt;
if(file_exists(fname)){ // Load existing json
var file = file_text_open_read(fname);
var json = "";
while(!file_text_eof(file)){
json+=file_text_read_string(file);
file_text_readln(file);
}
file_text_close(file);
map = json_decode(json);
wordList = map[?"list"];
}
The text file above uses JSON formatting, which defines objects as anything between {curly brackets} and lists as anything between [square brackets]. Game Maker natively understands how to convert JSON formatted stuff into data structures, so the code provided results in a ds_list data structure with all of your words in it, named wordList.
The formatting is slightly less forgiving than the above suggestion (every word on a new line), but has the potential advantage of organization. For example, it's possible to store multiple lists in a JSON file. In the example provided, there's 1 list, named "list", but it would be simple to make a second list by simply adding it to the file. If, for example, you needed a list of nouns and a list of verbs:
Code:
{
"nouns" : [ "noun1", "noun2", "noun3", "noun4" ],
"verbs" : [ "verb1", "verb2", "verb3", "verb4" ]
}
Then, using the code I supplied above (which simply reads the content of the file into a string and converts it to data structures, looking up each list is simple. Each list is stored in a structure named "map", so...
nounList = map[?"nouns"];
verbList = map[?"verbs"];
The 1-word-per-line solution does work--you'll open the file for reading and loop through each line like my script, but instead of adding each line to a string of JSON, you'll add each line to a ds_list structure. The primary difference is that you'd have to have separate files for each type of list you might need.