Saving Game Progress

There are multiple ways games can save a player’s progress. I’m not familiar with any secret methods for this, so as far as I know the methods are fairly straight forward and are likely similar in computer science.

The first thing you need to know when making a save and load system is how the system you’re using deals with reading and writing to files.


Know How Saving Works

A very easy way of creating save files is using text documents. These have no encryption normally, so they can be easily edited and given to other people.

Note that here loading is only covered in relation to save systems, and is not gone into in depth. One should know both how saving and loading works though before making a save system.

In Gamemaker, the function

1. file_text_open_write(fname);

opens a file with the name that you insert into the function. If the file name does not exist then it will create a text document with that name. How to use this function can be found here.

Once the file has been opened, the system waits for you to write to the file. Writing to the file is done with the following two functions:

2.file_text_write_string(fileid, str);

3.file_text_write_real(fileid, val);

These two functions allow you to take either a string (words or letters) or a real number (any type of number that is not imaginary) and write them into the current point the file is opened to. You can click on the functions to go to the GameMaker manual page to see how to use them.

Once you have opened the file using function 1, you can then immediately use either functions 2 or 3 to write a value into the file. While it is possible to write multiple things on one line, this article will avoid that and instead write one value per line of the text document.

At this point, you have opened or created a .txt format save file, and saved a single value in it on the first line. Since we are avoiding writing multiple things on one line, we will use the following function to go to the next line:

4.file_text_writeln(fileid);

Using functions 1-4, one can use the following code to save values to a file. The text in italics after two slashes “//” indicate comments explaining what the code does, they are not part of the code.

///Save Game

save_file_name=’savegame1.txt’ //Name of save file

save1=file_text_open_write(save_file_name) //Open file

if save1>-1 then //Check if file was properly opened

{

file_text_write_string(save1, player_name); //Save the player name as a string on the first line of the save file

file_text_writeln(); //Go down one line

file_text_write_real(save1, player_level); //Save player level on the second line

file_text_writeln(); //Go down one line for no particular reason other than in case I add another variable to save after this one later

file_text_close(save1); //Close the save file to reduce memory use and avoid errors

}


Ordered list

Ordered lists are simply a document with every value you wish to save either on separate lines, separated by commas, spaces, or anything else.

Advantages:

  • Simple to program
  • Simply to run

Disadvantages

  • Hard to read the file yourself when opening it outside of the game- If you try to edit the save file in a text editor, you might have to sheck the save function to see which line the variable is on. It would be very hard to identify the variable if it is for example a number similar to the other numbers around that line.
  • Order dependent- the order in which you save the values into the save file has to be the same order you load the values into the game. If you do not load them in the correct order, you might accidentally load the player’s gold value as their level and vice versa, which would change their game experience. You could also accidentally try to load a string when you are actually loading a real number, which would cause an error in the loading.

This will also cause issues when adding extra values to the save/load system. To show this, let’s say we currently are saving the following variables to a file:

  • Player Name: player_name
  • Player Level: player_level

The save file would look like this after saving the game:

slizer88

12

If we then wanted to save how much gold the player has, we would have to add that value at the end of the file. If we save the player’s gold amount between the player name and player level, then the save file will look like this:

slizer88

1337

12

As long as the new load function knows that the second line of the save file now indicates player gold, there should be no issues other than having to edit the load function as well as the save function. The problem is will different versions of the game. If someone updates their version of the game, and you changed the save and load functions to treat the second line of save files as player gold, then this person will start their updated game, which will load their old save file. This old save file would still have the second line of the save file a “12”, so the new version of the game will load 12 as the player gold, and will not load any value for the player level. This could either cause an error, or just have the player start at the first level with only 12 gold instead of 1337.

What this means is that if you use an ordered list, it is best to keep the same order of variables to keep the save files consistent. The problem with this is that your save file might be very disorganised.

Say your save system saves the following variables:

  • Player Name: player_name
  • Player Level: player_level
  • Player Gold: player_gold
  • Game Resolution: resolution_x
  • Windowed Mode: windowed

As you can see here, the first three variables are related to gameplay, while the next two variables are related to graphics. If you wanted to now add more gameplay variables to the save file, they would have to be saved after the graphics one, separated from the first three gameplay options:

  • Player Name: player_name
  • Player Level: player_level
  • Player Gold: player_gold
  • Game Resolution: resolution_x
  • Windowed Mode: windowed
  • Player Strength: player_str

With long save files, the position of various variables might be scattered and unorganised in the save file. While this doesn’t affect gameplay or the user experience at all if done correctly, it is preferable not to do this to make the save and load systems easier to make and easier to work with.


Keywords

An alternative to ordered lists are keywords. A keyword driven save file would look like this:

Player Name
slizer88
Player Level
12
Player Gold
1337
Game Resolution
1080
Windowed Mode
0
Player Strength
9001

Instead of loading one line at a time like in ordered lists, the load system reads a line of the file to determine what the next line’s value corresponds to. The script for loading would look like this:

///Load Game- Keywords

save_file_name=’savegame1.txt’ //The name of the file to load from

save1=file_text_open_read(save_file_name) //Load the file

if save1>-1 then //If the file exists then load

{

i=0

do{ //Loop through every line in the save file until it reaches the end

variable_name = file_text_read_string(file); //Read the current line of the save file, which contains the variable name keyword

file_text_writeln();

//Now that the variable name has been read, we can figure out which variable it is and save the next line into that variable

if variable_name=’Player Name’ then

player_name = file_text_read_string(file);

else if variable_name=’Player Level’ then

player_level = file_text_read_real(file);

else if variable_name=’Player Strength’ then

player_str = file_text_read_real(file);

file_text_writeln();

}until file_text_eof(fileid); //End loop when it reaches the end of the file

file_text_close(save1);

}

Notice here that the order in which you save variables does not matter. You can reorganise variables in any way you wish. The script reads player name, player level, and then player strength. When the script gets to the line of the save file that says “Player Strength”, it will save it to the correct variable.

The only issue with this is that it still might not load all the variables if the new version of your game saves and loads more variables than previous versions.


Encryption

Neither of these systems listed have encryption. While ordered lists are hard to read outside of the game, they are far from impossible to do with enough time and/or effort. I do that all the time to give myself more money in BMS, for testing purposes, of course.

There are many ways to encrypt files, including cryptography, hiding the file in a hidden folder or making the file invisible, and saving it in a format that cannot be read by most programs outside of the game. This is really only important if you really do not want players to edit their save files. This could be for balance reasons, especially for online games.

There are likely other ways to save games other than the ones listed here.

So that’s how you save things.

Advertisements
Tagged with: , , , ,
Posted in Game Philosophy
[DWN_LOAD/DIRCT]
Slizer BMS v1.17b1
[DWN_LOAD/SITE]
Slizer Battle Management System Windows game
[FILTER_POSTS]
[TWTR]
%d bloggers like this: