Trouble with external .txt files
q = file_text_open_read(working_directory + "anim.txt");
file_text_read_string(q);
if q == -1 show_message("oops");
So these are the only lines of code I wrote in my empty project, because I wanted to try out the use of .txt files.
However it doesn't work. An error comes up stating "File is not opened for reading."
If I click on the "Ignore" button, and keep running my game, the message "oops" is shown.
These elements make me think that Game Maker cannot locate my file, called "anim.txt", inside the so-called working_directory.
I did a little bit of research to make sure what and where the working_directory was in Windows 10, and what I found was confusing. Apparently it is supposed to be located at
C:\Users\Username\Appdata\Local\GameName
However, there is no folder with the same name as my game inside "Local". Instead there is a variety of folders named "gm_ttt", which contain a number of other folders. Inside some of these, I did find one with the name of my game, and put the file "anim.txt" there. But the error persists as I described at the start.
I found out that every time I run my game, new "gm_ttt" folders are created, and even if I put a copy of "anim.txt" in each, whenever I run the project Game Maker makes a new one (without my file in it), and proceeds to complain that inside the so-called working_directory, the file can't be found.
In short, I think in all this mess of folders named "gm_ttt", I am not finding the correct working_directory. Where and which is it, exactly?
Thanks in advance for any help!
Posted Feb 16, 2017, 2:19 PM UTC
A few problems I see here:
You're not closing the file after opening it. GMS has a limit of how many files it can have opened simultaneously, which you'll easily hit in under a second if a new file is being opened every Step or something.
You're reading a string from the file, but not putting it anywhere. file_text_read_string must be accompanied by a variable to be very useful.
The returned string of an empty file will be "", not -1
There's no need to look so hard for the working directory. In the Windows tab for global game settings you can choose whether to store local data in %appdata% or %localappdata% and by setting the name of your project you're deciding which subfolder to create in that directory. Or if you're still unsure, just draw working_directory as a string.
There's also no real need to add working_directory to your path for anim.txt as the working directory is assumed. Because GMS is sandboxed, if you create a file with no path it will automatically go in the working directory. The random gm_ttt folders you're getting may be a result of actually getting too specific with your path name. If a text file doesn't exist when you open it, it will be created.
Try:
EDIT: Something else to keep in mind is that working_directory refers to multiple places. If you're trying to read a text file that you've created beforehand, put it in your project's Included Files directory. That is the first location the working_directory constant checks.