Trouble with external .txt files

Resolved
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

210
LukeLC•

A few problems I see here:

  1. 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.

  2. 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.

  3. The returned string of an empty file will be "", not -1

  4. 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.

  5. 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:

q = file_text_open_read("anim.txt");
s = file_text_read_string(q);

if (s == "") { show_message("oops"); }

file_text_close(q);

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.

3
damimpIt just doesn't work, you know?•

The local folder doesn't get created until you make it yourself or create a file and write something to it within Game Maker. Just make that file path and drop the txt in.

If you're only reading from it, you can also include it directly in the project folder itself instead of in your app data.

2
microCACTUSOP•

Hello, thanks for the reply!

The name of my project is simply "cart".

I have created a folder called "cart", a folder named "cart.gmx", a folder called "cart.project" and a folder named "cart.project.gmx" inside C:\Users\Username\Appdata\Local.

I have also put my file named "anim.txt" in all of them.
After that I put ANOTHER copy of "anim.txt" in this folder: C:\Users\Username\Documents\GameMaker\Projects\cart.gmx

I still get the very same error. What am I missing? Game Maker can't seem to find the file.

1
naddercrusher•

Torey0 is right.

You should create the file from within GMS, not externally.

This happens because whenever you change the game, it's compiled as a different version, hence a different to_try folder.

It's only once you've compiled and published your game that the working directory will remain constant. Also, the documentation is out of date a bit, I remember when I was trying to do similar things that the actual working directory was different to what the documentation said it should be.

2
torey0sometimes helpful•

Use file_text_open_write to make a file and see where it ends up. You could also check if you need a slash after working_directory. Personally I don't use working_directory since the default path is in the "sandbox" the GM file functions let you access anyway.

2
microCACTUSOP•

I'm sorry I took this long to respond, I did some more testing.

I created an entirely new project, with a single object executing a single line of code for the Event "Create":

file_text_open_write("findme.txt");

I don't know why or how, but this file doesn't seem to exist in any folder, neither in
C:\Users\Username\Appdata\Local
nor in
C:\Users\Username\Documents\GameMaker\Projects.

It is the only line of code I wrote in the entire project, is it wrong, or am I missing something else?

---EDIT---
I added another line of code.

file_text_open_write("findme.txt");
if not file_exists("findme.txt") game_end();

Now the game shuts down as soon as I run it.

1
torey0sometimes helpful•

Also add a file_text_write_string(...) to put something in the file, then file_text_close(...) as well to make sure it saves it correctly. Sorry if I'm off on the function names but they're something like that. Try searching again once you get this working.

2
microCACTUSOP•

I just tried, but unfortunately I can neither write in the file nor close it, I get an error saying "File not opened for writing."
This error occurs twice now, the first time when I try to write inside the file, the second time when I try to close it.

The current code I have is as follows now:

file_text_open_write("findme.txt");
file_text_write_string("findme.txt","hello");
file_text_close("findme.txt");
if not file_exists("findme.txt") game_end();

If I ignore the errors and move on, then the game shuts down as soon as I run it, as it did before.

1
torey0sometimes helpful•

Try the following instead, and give those functions a quick read in the documentation. They want a reference to a file opened by the first function rather than the path again.

var file = file_text_open_write("findme.txt");
file_text_write_string(file,"hello");
file_text_close(file);
2