Since I have been working on a level editor for my game for the past few days, I thought I’d share a bit of the code that I came up with regarding saving and loading files from within Game Maker (GM). While none of the code requires the Pro version of GM, you will probably need to have worked with the built in language a fair amount already in order to understand the way I went about solving an issue I had.

For my level editor, I have already programmed a saving and loading feature for it that uses the function get_save_filename() and its loading counterpart. In the script used for saving a level, I first create a temporary variable called “filepath” that retrieves the name, directory, and extension from what the player entered. Some of the code can be seen below. Anyway, after having the program make sure that the player didn’t cancel, and also checking for an extension and adding one if none exists, the program will set the variable “global.filepath” equal to the temporary variable “filepath.”

1. var filepath;
3. filepath = get_save_filename(“Level Files|*.lvl”,””);
5. if!(filepath=””)
6.       {
7.       global.filepath=filepath;
8.       global.filepath=filename_change_ext(global.filepath,”.lvl”);
9.       }

While all of this is well and good, I had a specific feature I wanted to add to the level editor — I wanted the program to draw the filename in the bottom of the screen with the rest of the menu. However, since the function get_save_filename() retrieves the entire directory of the saved file, and not just the filename, I couldn’t have it only display the filename. After thinking the issue over for a little while, I came up with the below solution.

1. if!(global.filepath=””)
2.       {
3.       count=0;
5.       stringlength = string_length(global.filepath);
7.       while!(string_char_at(global.filepath,stringlength-count)=”\”)
8.             {count+=1}
10.       filename=string_copy(global.filepath,stringlength-count+1,stringlength);
11.       }
12. else
13.       {filename=”file not saved!”}

The if statement on the first line just checks to make sure that there is indeed a filename, and if not, on line thirteen, it will tell the user that the file has not yet been saved. In a second I will explain what the variable “count” will do, but first let’s jump ahead to the fifth line. There another variable is initiated, called “stringlength.” It is set equal to the total number of characters present in the filepath, using the function string_length(). Then, on line seven, a while statement is used to count backwards from the end of the string until it finds the last backslash (the “\”), signifying that it has covered everything other than the filename and extension. For instance, if the directory was like the example below, then it would stop once it reached the backslash right before “Yet Another Level.lvl”, making the variable “count” equal to twenty-two.

C:\Documents and Settings\Administrator\My Documents\Game Maker\New Game\Levels\Yet Another Level.lvl

And, if the character that the function string_char_at() has found is NOT a backslash (the “!” denotes a not statement), then it just adds to the variable “count” and repeats the function again. This will loop until it finds the whole filename, which in our case would be twenty-two loops later. Once the whole filename has been found, then the program can proceed in setting the variable “filename” equal to its findings. Using the string_copy() function, it copies everything in the string “global.filepath” starting from the end of the string (stringlength), subtracting the length of the filename (count), and adding one to it. If you don’t add one, then filename will equal “\Yet Another Level.lvl.” By adding one more, you remove the backslash that you found originally from the filename. The variable “stringlength” is then used one last time as the end point of where to copy. If you remember, back at the beginning of the code on line three the variable “count” was set equal to zero. That is to reset the variable so that you can repeat the counting loop from the end of the string instead of running into errors. And that’s it at last! You have now discovered the name given by the user.

You can now see this code in action below in three screenshots that I compiled from my game.

———————————————————————————————————      ——————

Chances are there was a much more simple way to do that, and I just overcomplicated things like I normally seem to do. So, if you find an easier way to do what I just walked you through, go ahead and tell me in the comments!

-Nathan Wood


About turboRamble

I am a seventeen-year-old who enjoys programming games and applications, as well as reading and writing.

One response »

  1. lernen says:

    Thanks a lot 🙂 for sharing it with us. Take care!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s