How to Make a Timer in GameMaker

The Game Maker development suite by YoYo Games is popular for its ability to let novice game developers create their dream games without programming knowledge. However, some game features don't have ready-made functions in the Game Maker interface. This is where the enterprising developer has to write his own. One of those instances is for a game timer that limits the player to a certain amount of time to complete a level.

Some games need a timer.

Step 1

Create an object by right-clicking on "Objects" and choosing "Create Object." Name the object "timer." Do not assign it a sprite.

Step 2

Click "Add Event" and "Create." This will create the code to occur as soon as the game starts.

Step 3

Go to the "Control" tab and drag the icon that looks like a square with the word "VAR" in it to the "Actions" panel. Doing this is the "set variable" action and you use it to store information for use elsewhere in the program. In the pop-up box that appears, give your variable the name "room_speed" and set its value to 30. This is telling the game you want it to operate at 30 frames per second. This is very important, since Game Maker only remembers the number of frames it shows and not how much time elapses.

Step 4

Drag another "set variable" action into the "Actions" panel. Name this variable "seconds" and set it to 10. This is the amount of time on your game's timer.

Step 5

Click the "Main 2" tab and drag the icon that looks like a stopwatch into the "Actions" panel. Change the "number of steps" setting to read "room_speed." Remember that "room_speed" tells the game how many frames will occur in one second. Now you are setting an alarm to go off after that many frames--in other words, to go off after one second.

Step 6

Click "Add Event" and choose "Alarm" to create the coding that will occur every second when the alarm goes off.

Step 7

Drag another "set variable" action and set the variable "seconds" to -1 and click the box marked "relative." This tells Game Maker to subtract one from the seconds variable instead of simply setting it to -1.

Step 8

Drag the test variable icon into the "Actions" panel. It looks like the word "VAR" inside an octagon. Set the variable to be tested to "seconds" and set the value to be tested for as "0."

Step 9

Go to the "Main 2" tab and drag the "End Game" icon into the "Actions" panel. It is the red button that looks like a power-off button. This will end the game when the seconds on the timer reach zero.

Step 10

Go back to the "Control" tab. Drag the "ELSE" icon into the "Actions" panel. This tells Game Maker what to do when the seconds are not zero and we are going to use this to reset the alarm. Go to the "Main 2" tab and drag the set alarm action into the "Actions" panel. Once again, set the alarm back to "room_speed."

Step 11

Click "Add Event" and select "Draw." The timer is fully functional but it would only be fair to let the player see it. Drag the "Draw Variable" button from the "Control" tab and enter "seconds" into the variable box.

Step 12

Create a room by right-clicking "rooms" and selecting "create room." Click anywhere on the room's grid to add a "timer" object to the room. Click the green check mark to "save" and click the green arrow to "run" your game. The game should run for 10 seconds, with a timer in the top-left corner of the screen. As soon as the timer hits zero, the game will end.

references & resources