Ben's Game Warehouse

Click here to edit subtitle

If you want to have the GMK for this tutorial, you can download it HERE.


This is a pretty long tutorial, and if you just want to see one thing, put these titles into find by hitting ctrl f

Variables and Why They're Important

Creating a Variable

Modifying Variables

Global Variables

When to Use Global Variables

List of GM Variables


Variables and Why They're Important

Variables are a means of storing data to be accessed and/or changed, and they're everywhere in games. Health, score, ammo, level, speed, etc. and all examples of variables in games. But there are other variables that you might not think of, like the current sprite, the direction your character is facing, your location on the x/y plane, your instance ID in relation to other objects, etc.

Today I'll be explaining how to create, access, and modify a variables, as well as show that we've already used variables in the last tutorial. So now, let's open back up to where we were before.

Creating a Variable

When you create a variable, you do it in a Create Event. So, let's add a create event top our player object and add some code.

points, (remember that this is case sensitive) is my new variable which I have set to 0. This is called Initializing a variable. Now I bet you're thinking, why use points when you could have used score? Score is a built in GM variable. This makes it a bit more restrictive, and because of this I don't use it.

Now that we have our new variable, let's start modifying it!

Modifying Variables

Back in our step event for the player object, add this line of code. What is it doing?

Every step the game checks whether the space bar is being pressed (note the use of keyboard_check_pressed). If it is, our points variable will increase by 1.

Before I get further into this, let's find out if this is actually working by taking advantage of GM's Debug Mode. Next to the green Run Game arrow, click the red Debug Game arrow.

When you first open up debug mode, this is what you should see. This allows you to keep track of variables in your game to find problems or see whether something is working correctly. 

First, add your variable by clicking on the green cross, then add your variable. Notice that I added obj_player. in front of the variable points. This is because points is a variable that is exclusively contained within the obj_player object. To access it from any other place that isn't the object it was created in, you need to add the domain of it's object, obj_player, and connect the two with a period

This is called a domain, we'll get more into variable domains soon. For now, let's test to see if this works. Hit the OK button and see if your variable was found correctly. If it shows the points variable equal to 0, you did it correctly. If it says ERROR, then you did not and should refer to the troubleshooting questions below.

1. Did you write your points variable exactly as you did when you initialized it? Including capitalization?

2. Did you add the object it was created in exactly as it was written as the domain?

3. Did you separate the domain and variable with a period? NO SPACES

Go ahead and hit the space bar (or whatever key you set it to) once. The points variable should change from 0 to 1.

Hit it 9 more times. points should now be equal to 10. If whenever you press the space bar, points goes up by 1, you've done it correctly. 

Alright, now let's try and modify a variable with another object.

Global Variables

Create a new object called obj_tester. It doesn't need a sprite or anything because it's only purpose will be for testing variables we create. Add a step event and paste the same space bar code we added for the player object to increase points by 1 into the tester object. Go ahead and put the tester object anywhere in our room. Now let's quickly run the game (no need for debug mode) and try pressing the space bar.

This will be the first of many error messages you will see in your time making games.

at position 39: Unknown variable points

But how can it be unknown if we initialized points in the player object?

Well, that's the answer. points, is a variable exclusive to obj_player. If we wanted to access points from obj_tester, we'd need to add the obj_player. domain like we did in debug mode. 

But I don't want to do that. I want to make points a variable accessible from any object. We can do that by changing points into a global variable

You need to go back to all instances of points and add the domain global. to each one. Now it should be global.points.

Go ahead and test the game again. If the tester works when you press space, you were successful. Also remember, if you want to use the debug mode for this variable, don't forget to add global. to points. It does matter.

When to Use Global Variables

You might be asking, why would I bother with this when I can just add the object domain when I need to access the variable from other objects? Sure, you can do that. But some variables are better being global. Generally things that are constantly being changed by many different objects such as player health should be global.

This is mainly for organization and is easier to understand. A global. variable is just that. It's it's own thing. Any other variable can have many different domains. And if you're constantly having to access and modify it, just make it global.

However, there are times when you don't want something global. Enemy health for example should not be global. Especially if you have a lot of different enemies. Instead of making hundreds of HP variables for hundreds of enemies. Just make one HP variable, and when you need to access a specific enemies HP, add the domain.

List of GM Variables

GM variables are game maker variables. We;ve already used two in the previous tutorial, hspeed and vspeed. So these should be pretty easy to get the hang of. This is a list of GM variables we'll be using throughout this tutorial series. Experiment and familiarize yourself with them.

x - The x coordinate of an object on the x/y plane

y - The y coordinate of an object on the x/y plane

mouse_x - The x coordinate of the mouse on the x/y plane

mouse_y - The y coordinate of the mouse on the x/y plane

hspeed - The horizontal speed of an object

vspeed - The vertical speed of an object

gravity - The gravity of an object (number added to an object in X direction)

gravity_direction - The direction of the gravity of an object in degrees (0 - 360)

friction - The friction of an object (number subtracted to an object in X direction)

direction - The direction in degrees (0 - 360)

sprite_index - The sprite of an object

image_index - The subimage of the sprite_index of an object

image_speed - The speed in which a sprite changes it's subimage (in steps)

There are probably more I'm forgetting, but we should use most or all of these variables.


Thanks for reading the second tutorial, the next one will be on collisions between objects and collision masks. Don't forget, if you're having trouble download the gmk from the top to see what you're doing wrong.