• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Loading a file with a variable name

J

James Manning

Guest
I'm new to GML and GMS so please forgive my basic knowledge (first proper attempt at a good game)

I have a set of files all containing words separated by word length in the included files.
eg. Words with 4 letters are all stored in a file called 4.txt
I want to load them 1 by 1 into a 2d array called words [ letters in the word , word ].

How can I create a script that can allow for them to be added as they are needed for the array?

So far I have:
Code:
//scrLoadWordFile(MaxWordLength)

maxlength = argument0; //Gets the word length desired


if (maxlength > 21) or (maxlength = 24) // I have files for 1-21 and then another for a 24 letter word
var filename = "/" + maxlength + ".txt";
{
    wordsfile = file_text_open_read(working_directory + filename); //Open the word file
    var num = 0; 
    while (!file_text_eoln(file)) //Read until end of file
    {
        game.words[maxlength-1,num] = file_text_read_string(wordsfile); //Read word into array
        file_text_readln(wordsfile); //next line
        num++; //increment num up to put word into next cell
    }
    file_text_close(wordsfile); //Close the word file

}
This code is run at the creation of the first room and shoots out this error.

ERROR!!! :: ############################################################################################
FATAL ERROR in
action number 1
of PreCreate Event
for object guiCounter:


DoAdd :: Execution Error
at gml_Script_scrLoadWordFile (line 6) - filename = "/" + maxlength + ".txt";
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scrLoadWordFile (line 6)
called from - gml_Room_room0_Create (line 2) - scrLoadWordFile(game.max_word_length);

I'm open to suggestions on how to do this another way (whether that be just using 1 file with sections or anything, I just thought this would be more efficient since it only calls the file when it needs it.
 

CMAllen

Member
One, I'm not sure you're using the correct slash. Two, unless you're navigating to a sub-directory inside working_directory, I don't think you don't need to add the slash manually. Finally, if your input argument is a real, you need to convert it to a string with the string() function before appending it to an existing string.
 
Top