One of the most useful parts of Game Maker, or any programming language, is how it handles variables. Wikipedia defines a variable as “a value that may change within the scope of a given problem or set of operations.”

Now I’m sure you’re thinking, “Variables? I learned about those back in high school.” Yes, I know all about how you had to find the values of x and y and whatever other letters. However, in programming, variables are used to give the player choices, health, ammunition, options, achievements, etc. Before we dive into all of that, though, let me explain the two most common value types that are used.

Constants and Variables

Constants are values that never change throughout the course of the program. You set them in the code for some reason or another, and then when you test it out their value remains the same. For instance, if you decide the player’s max health should always be set equal to 100, you don’t need to create a variable for it. That number will always be constant.

However, variables are values that can be changed by you in the script or the player in-game. You can create a variable called “health” and set it equal to the player’s max health (100 for this example) at the start of the game. Then the player gets attacked by an enemy, then what happens? You need to subtract a constant from their health. If you want the enemy to damage 10 of the player’s health, then you subtract 10 from your health variable.

obj_player Create:
1. health = 100;

obj_enemy Step:
1. if place_meeting(x,y,obj_player)

2.       { -= 10; instance_destroy()}

By putting the code above into the “Step” event of your enemy, you can have them deal damage, changing the variable and then killing themselves. If the player’s health had been equal to 100/100, then their health would now be 90/100. The constant on the right would always stay the same, while the variable to the left will change depending on what happens in-game.


Boolean is a true or false variable. One of the many possible uses for these would be to check if the enemy has damaged the player yet. Perhaps you don’t want the enemy to die when it comes in contact with the player, but you also don’t want it draining the player’s health each step. Here’s where a Boolean can help you. We can create a variable called “damaged” in the “Create” event of obj_enemy and set it equal to false.

obj_enemy Create:
1. damaged = false;

Pretty simple so far. Now, in order to make this variable do something, we need to add it to the “Step” event code that we created earlier. First we need to check if the variable damaged is equal to false, and if it is, then subtract 10 health from the player and also set the Boolean damaged equal to true so that the event cannot repeat.

obj_enemy Step:
1. if place_meeting(x,y,obj_player) and damaged = false
2.       { -= 10; damaged = true}

Now the enemy will survive but no longer be able to damage the player. Maybe you want to make it so the player can be damaged by the enemy again, but only after a certain amount of time? We have a variable for that! We need to declare two more variables in the “Create” event of obj_enemy, but this time call them “timer” and “start_time“. For this example, I’m going to set start_time to 80.

obj_enemy Create:
1. damaged = false;
2. timer = 0;
3. start_time = 80;

Lastly what you want to do is implement these variables into the “Step” event for obj_enemy. What this means is if the enemy has recently damaged the player, it won’t be able to until after timer equals the value of start_time. Once this occurs, then timer is reset back to 0 and damaged is equal to false so that the player can take damage again.

obj_enemy Step:
1. if place_meeting(x,y,obj_player) and damaged = false
2.       { -= 10; damaged = true}
4. if damaged = true and timer != start_time
5.       {timer +=1}
6. else
7.       {damaged = false; timer = 0}

There you have it! Now you have a code that checks to see if the enemy has damaged the player yet and if the timer has run out yet. If the timer has run out, then it resets everything again so that the enemy can continue damaging the player. You just used variables! If you have any questions or comments about this, just tell me in the comments below. I am ready and willing to help you if you have trouble understanding how all of this works.

-Nathan Wood


About turboRamble

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

4 responses »

  1. Anne says:

    Truth Or dare game

  2. I have one; could I just put the damaged = false etc code into the player object to ensure the player can’t get smothered by several enemies from multiple directions?

    • turboRamble says:

      Yes, I would actually recommend that. Normally you will have less player objects than enemies, so it’s better to have the code in the player. This is especially true with collision detection as it can be a heavy performance hit.

      You can give each enemy a damage amount, so that when the player detects a collision with an enemy (and the player hadn’t been damaged recently) you can do something like health -= other.damage; damaged = true

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