Learn the Game Maker Language (GML) through tutorials and examples

Game Maker Language Tutorials and Examples

Text Files

So we just talked about INI files and I told you that INI files are great for saving games and settings. So, what would you use text files for? Well, to answer simply, text files are great at storing huge amounts of data, such as data used to create a level. One of the nice things about text files is that they can hold thousands of lines of data without the annoying use of sections and keys. Instead of giving a key to every individual piece of data, text files simply just write the data as is in a text file.

Here's how text files work: 1. You open the file. 2. You read/write one line in the text file. 3. You then tell Game Maker to read/write another line until you reach the end of the file or are finished writing data. 4. You close the text file. Here's how you could write in a text file:

txtfile = file_text_open_write("textfile.txt"); /*file_text_open_write(fname)...opens file fname for writing; if fname doesn't exist, it will be created*/
for(i=1;i<=5;i+=1) { /*This for loop will write the numbers 1 through 5 in the text file*/
file_text_write_real(txtfile,i); /*file_text_write_real(opened_file,value)...writes real value value in opened_file text file (in this case, txtfile)*/
if i<5 {
file_text_writeln(txtfile); /*file_text_writeln(opened_file)...writes a new line in opened_file text file (txtfile). This bit of code in the brackets simply makes sure that we don't write a line that doesn't have anything on it*/
file_text_close(txtfile); /*file_text_close(opened_file)...closes opened_file text file. Always do this after you are done with text files!*/

Now if you save this as a game, you will see that a text file called "textfile.txt" will be created in the same folder as your game. Within this text file, you will notice that the numbers 1 through 5 appear, with each number having its own line. Okay, great, but how do we retrieve that data back in Game Maker? Like this:

txtfile = file_text_open_read("textfile.txt"); /*file_text_open_read(fname) opens file fname for reading*/
n = 1;
do {
read[n] = file_text_read_real(txtfile); /*file_text_read_real(opened_file) reads a real value from the line that Game Maker is currently on in the text file from the opened_file text file*/
//The use of the array read[] is to capture all of the data
n += 1; //For the array...
file_text_readln(txtfile); /*file_text_readln(opened_file) goes to the next line in the opened_file text file*/
} until(file_text_eof(txtfile)); /*file_text_eof(opened_file) returns whether we have reached the end of the text file*/
str = "";
for(i=1;i<n;i+=1;) { //This for loop will read all of the data saved in the read[] array as a string
str += string(read[i])+"#";
show_message(str); //This will then show us the values read from the text file

Phew. What just happened? Well let's look at the first section of code to start with. What we did was we opened our text file and then told Game Maker to read that text file line-by-line and save each of those lines in part of an array. Game Maker was told to keep reading that text file until it reached the end of the file. Once we had, we closed the text file. After that, we simply created a new string, str, and added to it the string values of the data contained in our array, read[], that stored all of the values that we read from the text file. Once the string was complete, we then ued the show_message() function to show us those values. See? All of the numbers 1 through 5 that we wrote earlier on were read again. If they were not, be sure to use the above snippet of code into the same saved game that you wrote the text file with.

Phew, well that's text files for you. Next up, user input.


Contact Dan Hubley: djh5331@psu.edu. Feel free to contact me for suggestions on tutorials and examples.

Beginner Tutorial Parts:

GML Intro
GML Part 2
GML Part 3
GML Part 4
GML Part 5
GML Arrays
GM Resources
GM INI Files
GM Text Files
GM User Input
GML Closing

First Game Tutorial

Power Ups
Closing GML


Missile Example
Missile Example
Race AI
Race AI
Mario Party
Missile Example2
Missile Example2
Point of View
Point of View
Mouse Follow
Password Creator
Circle Targeting