GMS 2 Contdown Timer && Count-Up Timer [GMS2]

PlayerOne

Member
GM Version: GMS2 (Unconfirmed on 1.4)
Download [GMS2]: https://drive.google.com/open?id=1xzRCMqFHCJbRk985W4ggXQzSHiSCljeF
Version: 1.0


Description:

This is how to make a countdown and count-up timer for your game(s).


TIMER.png

Code:
What is needed to proceed:
-Knowledge of GML.


To start we will create a countdown clock and worry about a count up clock when were near the end of this tutorial. Create a blank object called clock_countdown and add the object variables _min and _sec.

If you’re wondering _min equals minutes and _sec equals _seconds.

Don’t get mixed up with the min with _min because there is a function called min in GML so take great care not to get that mixed up.

Code:
CREATE EVENT

//Clock setup.
_min=1; // <<< This indicates minutes.
_sec=0; // <<< This indicates seconds.

With that set let’s go into the step event. I won’t go into complete detail but suffice to say you can read the code to understand how it counts down and what happens when it reaches 00:00.

Code:
STEP EVENT
if (_min==0 && _sec==0)//<<< The time you set hits the limit!
{
//Do Something when times up!  
}
else
{
                if (_sec==0)
                {           
                _min-=1 // Minus 1 minutes
                _sec=60 // Add 60 seconds!
                }
                else
                {
                _sec-=1 // If it doesn’t equal 0 continue to subtract!
                }
}

Next add a Draw GUI to the object and have a draw_text code with it having X equaling 50 and Y equaling 50. This is the complicated part as this will draw the clock in certain way when it reaches a certain point in the timer.

Again this part can be a headache when you look at it but this will draw a 0 in a place to make it look like it gives a timer like effect when minutes or seconds reach below 10 and when both _sec and _min equals 0.

var _xx and var _yy link the draw_texts to allow anyone to move the timer seamlessly without worry.

Mind if you want the timer to move in game just add the _xx and _yy to the create event and not make it a var.


Code:
DRAW GUI

//This links the multiple draw texts to create the timer!
var _xx = room_width/2-25;
var _yy = room_height/2;

if (_min==0 && _sec==0) // if text equals 00:00
{ 
   //This indicates the time is  00:00.
   draw_text(_xx,_yy,string("0") + string(_min) + string(":") + string(_sec) + string("0"));
}
else if (_min>=10) // if text is greater than 10 _min
{
   if (_sec>=10) // if _sec equals or is greater than 10
   {
       draw_text(_xx,_yy,string(_min) + string(":") + string(_sec));
   }
   else if (_sec<=9)  // if sec is less than 9.
   {
       draw_text(_xx,_yy,string(_min) + string(":0") + string(_sec)); 
   }
     
}
else if (_min<10) // if min is less than 10.
{
   if (_sec>=10) // if sec is greater than 10.
   {
       draw_text(_xx,_yy,string("0") + string(_min) + string(":") + string(_sec));
   }
   else if (_sec<=9) // if sec is less than 9.
   {
       draw_text(_xx,_yy,string("0") + string(_min) + string(":0") + string(_sec)); 
   }
}
Now onto the count up timer. Same principle as above the difference is you have to manually set the timer where you want it. Say you want to count up to 2 minutes and 30 seconds it will stop at that time and then something happens.

To start set _min and _sec to zero and add two new object variables to _time_min and _time_sec to the create event. The last two object variables set the time you want to set when the count-up timer hits a certain time.

Code:
CREATE EVENT

//Clock setup.
_min=0; // <<< This indicates minutes.
_sec=0; // <<< This indicates seconds.


//This indicates the time you set for the timer.
_time_min=2; //_time_min being the minute on the clock.
_time_sec=30; //_time_sec being the second on the clock.

Code:
STEP EVENT (COUNT-UP CLOCK)

if (_min==_time_min && _sec==_time_sec) //<<< The time you set hits the limit!
{
//Do Something when time hits the limit!           
}

else
{
                if _sec==60
                {           
                _min+=1
                _sec=0
                }
                else
                {
                _sec+=1
                }          
}


Add this to the draw event. The last draw_text indicates the time you set hit its mark.


Code:
DRAW EVENT

//This links the multiple draw texts to create the timer!
var _xx = room_width/2-25;
var _yy = room_height/2;

if (_min==0 && _sec==0) // if text equals 00:00
{ 
   //This indicates the time is  00:00.
   draw_text(_xx,_yy,string("0") + string(_min) + string(":") + string(_sec) + string("0"));

}
else if (_min>=10) // if text is greater than 10 _min
{
   if (_sec>=10) // if _sec equals or is greater than 10
   {
       draw_text(_xx,_yy,string(_min) + string(":") + string(_sec));
   }
   else if (_sec<=9)  // if sec is less than 9.
   {
       draw_text(_xx,_yy,string(_min) + string(":0") + string(_sec)); 
   }
     
}
else if (_min<10) // if min is less than 10.
{
   if (_sec>=10) // if sec is greater than 10.
   {
       draw_text(_xx,_yy,string("0") + string(_min) + string(":") + string(_sec));
   }
   else if (_sec<=9) // if sec is less than 9.
   {
       draw_text(_xx,_yy,string("0") + string(_min) + string(":0") + string(_sec)); 
   }
}

//THIS ALERTS YOU THAT THE COUNT-UP TIMER HAS HIT ITS MARK!
if (_min==_time_min && _sec==_time_sec)
{
draw_text(_xx,_yy+25,"TIMES UP!")     
}


That’s it in terms of the countdown timer and count up timer. Now you can set it like this and you can be on your way but there is one more thing to do and that is tie the _sec and _min to your room_speed. In the step event add the following lines to each of the object variables -- /room_speed*room_speed.

In the event your room speed takes a hit performance wise the timer will count down or count up normally (correct me if I’m wrong on this)


Code:
STEP EVENT (COUNTDOWN CLOCK)

_min-=1/room_speed*room_speed;
_sec=60/room_speed*room_speed;
_sec-=1/room_speed*room_speed;

Code:
STEP EVENT (COUNT-UP CLOCK)

_min+=1/room_speed*room_speed;
_sec=0/room_speed*room_speed;
_sec+=1/room_speed*room_speed;
 
Last edited:
Top