Gw Temp

Menu

Tutorial - 'Basic GM Database' by Typer85

An item about Gamemaker posted on

Blurb

Typer85 gives a very good explanation for newer Game Maker users on how to create Databases with GM to save information for when the game is exited and then reopened.

Body

Table of Contents:
I. Introduction
II. What is a Database
III. What You Need
IV. Tutorial Part I
V. Tutorial Part II
VI. Testing
VII. Tricks
-----
I.
This tutorial is written to help new Game Maker (GM) users make a very simple Database.

Before I start, I would like to thank GZ_Soft from the GM Forum for helping get a hands on start with GM and making a Database. It is because of him that this tutorial exists.
-----
II.
A Database is just what it sounds like. It is a file, usually a TXT file, that stores information. In GM, information is saved in variables; however, once the program is exited, this information is gone. A Database helps store valuable information, like a player's in-game name, so whenever the game is exited or loaded up, that information will always be there.
-----
III.
Before you go ahead and read the rest of this, you must make sure you have a basic understanding of variables.
-----
IV.
a.
Go to the directory where you know you will save a new GMD file for a new project you will start in this tutorial. In that same directory, create a new TXT file. Save the file as 'data.txt'. This is done by opening Notepad. Make sure you do not type anything in the file before or after you save.

b.
Start up GM. Start a new project. I usually do not recommand working on a saved project, until you have a basic understanding on how this works, so you do not ruin any aspects of your current project.

c.
Save the project right now, in the directory where the TXT file we created before is saved.

d.
Create a new Object and call it 'obj_player'.

e.
Create a new Create Event and add Execute a Piece of Code for its action.

Once in the code editor, add the following code:

//Start Code
data_write();
//End Code

What this does is run a script called 'data_write', which we will create in a little while.

f.
Create a new Draw Event and add Execute a Piece of Code for its action.

Once in the code editor, add the following code:

//Start Code
data_read();
//End Code

What this does is run a script called 'data_read', which we will create in a little while.

This is just the basic beginning of the Database. Read on for the hard work.
-----
V.
Now to the actual editing of the Database.

a.
Go to Add Script. Name it 'data_write'. In the text area of the script, add the following code:

//Start Code
file_open_write('data.txt');
file_write_string('Alexander');
file_writeln();
file_write_string('Male');
file_close();
//End Code

Now what this does is this. It opens the file 'data.txt', on the first line it write 'Alexander', skips one line down, on the second line it writes 'Male', then it closes the file, thus saving it.

Notice the '' I used between the (). It is very important to specify the information you want to edited or opened in '', or GM will think your calling a variable and return an error with a message saying unknown variable.

b.
Go to Add Script. Name it 'data_read'. In the text area of the script, add the following code:

//Start Code
file_open_read('data.txt');
player_name=file_write_string();
file_readln();
player_gender=file_write_string();
file_close();
//End Code

Now what this does is this. It opens the file 'data.txt' to be read, saved the information written on the first line in the variable 'player_name', skips one line down, saves the information on the second line in the variable 'player_gender', then it closes the file, thus saving it.

We just created a very small Database. So far all it does is write two entries and read them back, saving them in variables. Read on for the testing.
-----
VI.
a.
Go back to 'obj_player' we created before.

b.
Go to the Draw Event we created before and add Execute a Piece of Code for its action. Once in the code editor, add the following code:

//Start Code
draw_text(50,50,string (player_name));
draw_text(50,60,string (player_gender));
//End Code

What this does is writes the value of the two variables 'player_name' and 'player_gender'.

Notice the numbers, that is just the X and Y coordinates.

c.
Place this Object on a New Room and test play your game. And volia, behold on your screen will be written 'Alexander' and under it 'Male'.

If you go to the TXT file, you will notice on the first two lines, 'Alexander' and 'Male' will be written.
-----
VII.
Writing Databases is quick to save time. However, some players might be able to easily open the TXT file and see the information in there. Encryption helps, but for new users, there is an easier way that might be helpful.

a.
Save the file in a format other than TXT, such as INI. You can even make up your own format, like HELL or YES. Might trick non-computer literate players.

b.
Make the file attribute Hidden. That way when you ZIP the file and a player extracts it, he will never see the actual file, thus believe there is no Database at all.

c.
Save the file in a different directory. When calling the file use something like this, X:\Directory1Directory2Data.txt, where X is the drive letter. Between each directory, make sure the first letter of every directory is capital.

d.
Be creative when creating Databases. You can easily hide the actual file anywhere on the hard drive.
-----
This is a very simple Database for brand new users. Databases can be so much more complex, storing anything from item attributes to the amount of time a game has been played.

I hope this tutorial gives a simple understanding on how they work. Take a look at the GM Help File for a list of commands. And if your ever stuck, visit the GM Forum. There are a lot of people on there that are more than glad to help.

Thanks for reading!