• 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!

Writing Text to a file

So I'm trying to mess about here and see if I can generate various kinds of files in the save directory for Easter Egg type purposes. I'm messing with file_write_text_string() and it's throwing an error that it wasn't expecting a string on a function to write a string?

GML:
file_text_open_write("story")
file_text_write_string("story", "writing to file to test")
file_text_close("story")
2021-02-28.png
 

TsukaYuriko

☄️
Forum Staff
Moderator
The error message is not talking about the text to be written to the file, but the other argument. This is supposed to be a file handle, and you're passing a string. The file handle is returned by file_text_open_write, and you're not assigning it to a variable and subsequently not using it to address it.
 
The error message is not talking about the text to be written to the file, but the other argument. This is supposed to be a file handle, and you're passing a string. The file handle is returned by file_text_open_write, and you're not assigning it to a variable and subsequently not using it to address it.
So then how do I format the file location? The instructions were a bit foggy on that the way they wrote the function in the sample script and nothing I try seems to work.

Edit: I got it, I didn't realize I had to do a save ID to file and then pass than down the code
2021-02-28 (1).png
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Not sure if that's what you meant with "save ID to file and then pass than down the code", but the manual has proper usage examples for these functions. Assign the return value of file_text_open_write to a variable, then use this variable to refer to the file in other functions rather than its file name.
 
From the manual:
Code:
var file;
file = file_text_open_write(working_directory + "\level.txt");
file_text_write_string(file, level_data);
file_text_close(file);
So the argument for file_text_open_write() is the path+name of whatever you want the file to be saved as. If you wanted your save file to be named "my_save.sav" you would input file = file_text_open_write(working_directory + "\\my_save.sav");. You've now got the internal GMS ID for that "my_save.sav" saved in the local variable file. You use this ID to actually write to the file. If you want to write your string to the file, it's as simple as file_text_write_string(file,"writing to file to test");. Then you simply close your file with file_text_close(file);.
 
Yes, you would use file_text_writeln(file); between your file_text_write_string(file,"writing to file to test"); to create new lines:
Code:
var file = file_text_open_write(working_directory + "\\my_save.sav"); 
file_text_write_string(file,"Line 1.");
file_text_writeln(file);
file_text_write_string(file,"Line 2.");
file_text_writeln(file);
file_text_write_string(file,"Line 3.");
file_text_close(file);
 
Not sure if that's what you meant with "save ID to file and then pass than down the code", but the manual has proper usage examples for these functions. Assign the return value of file_text_open_write to a variable, then use this variable to refer to the file in other functions rather than its file name.
I mistyped

I meant store file ID as a variable at creation and pass that to the other txt functions.
 
You wouldn't store the GMS file ID, you would simply store the path and then use that stored path to open the file when you want to write (it's usually not a good idea to keep files open over extended periods of time). So you would do something like this:
Code:
global.save_file = working_directory + "\\my_save.sav";
Then whenever you wanted to write something, you would do this:
Code:
var file = file_text_open_write(global.save_file); // Might want to use file_text_open_append(global.save_file); if you want to continue writing from the end of the document
file_text_write_string(file,"Saving a string");
file_text_close(file);
 
Top