Tutorial: Creating a timer

Learn how to make certain types of games and use gameEditor.

Tutorial: Creating a timer

Postby SuperSonic » Fri Dec 30, 2011 4:14 am

Hey guys!

So I was working with Phyzix and he wanted me to create a timer for his game that displays minutes and seconds and decreases every second. I had never done anything like it but luckily, with the help of skydereign, I was able to succesfully create a timer. Now I would like to share my knowledge with you guys. So here is a tutorial on how to create a timer in your game. I hope you enjoy :D

Setting up the global code
Ok so the first step in making a timer would be to initialize a few variables and to do that, we need to use global code. So go ahead and open it up and create the following variables (You will also have to initialize them):
Code: Select all
int SecLeft = 600;
int VirtMin = 10;
int VirtSec = 0;
int NumFrames = 0;

Ok so let's go ahead and figure out what each variable will do. The first is "SecLeft". This will be the amount of seconds left in your timer. I've made it equal to 600 for this example (that's 10 minutes for those of you keeping count =D). The next two are "VirtMin" and "VirtSec". These stand for "virtual minutes" and "virtual seconds" and they are the variables that will be displayed by our timer. The last is "NumFrames". This is the number of frames that have gone by. You will see why this is important later :wink:

Setting up the timer actor
Now, we will need an actor to display our timer as well as handle our variables. So let's create an actor and call it "TimerActor". Now the first thing to do is to open the actor's control panel and select text. Next, enter in any string of text and make sure to select a font to use. Now our actor is a text actor. The next step is to create a "Create actor" event. Now in the event put this:
Code: Select all
sprintf(text, "%i:%i", VirtMin, VirtSec);

Now this bit of code may be a little confusing so I'll go over in detail. First, you have the function "sptintf()" which will print the time to the actor's text. Now do you notice the two %i's? Those mean that the computer will be printing two integers to the actor's text. Also notice that there is a colon in between them. Finally, you should see that I have added VirtMin and VirtSec as parameters. So what this means is that it will print the minutes and seconds divided by a colon. I hope that made sense :D

Now for the really hard part. Create a "Draw actor" event for the timer actor. Now the code for this part is long and complicated so I'll give you the whole thing but then go back and explain each line to you :P
Code: Select all
if(NumFrames >= real_fps)
{
    if(SecLeft > 0)
    {
        SecLeft -= 1;
        if(VirtSec <= 0)
        {
            VirtMin -= 1;
            VirtSec = 59;
        }
        else
        {
            VirtSec -= 1;
        }
        sprintf(text, "%i:%i", VirtMin, VirtSec);
    }
    NumFrames = 0;
}
else
{
    NumFrames += 1;
}


Now for the explaining part:
Code: Select all
if(NumFrames >= real_fps)
{

This checks to see if a second has passed. If it has then it does this next bit.
Code: Select all
if(SecLeft > 0)
{

Now it checks to see if there is still time left. If so then...
Code: Select all
SecLeft -= 1;

Now it takes one second away from the time.
Code: Select all
if(VirtSec <= 0)
{

Now you should remember that VirtSec is the seconds that get displayed. If you have seen a normal timer, then you know that once the seconds reach 0, the minute decreases and the seconds go back up to 59. This is exactly what happens in the next bit of code.
Code: Select all
VirtMin -= 1;
VirtSec = 60;

}
Now this is the next bit of code.
Code: Select all
else
{
VirtSec -= 1;
}

So if the virtual seconds are not equal to zero, then it just takes one away.
Code: Select all
sprintf(text, "%i:%i", VirtMin, VirtSec);

Now I've already explained this before so you should be able to know what it does.
Code: Select all
}
NumFrames = 0;
}

So remember that in the beginning, we had an if statement that checked to see if a second had passed? Well, it did that by checking if the number of frames that had passed was equal to our framerate. So we need to set that back to zero or nothing else will happen.
Code: Select all
else
{
NumFrames += 1;
}

Now if a second has not passed, then it will just add one to the number of frames that have passed.

I hope this tutorial was useful. If I made a mistake in my code or if you are unsure about something, please let me know :wink:

-Sonic
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1444
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial: Creating a timer

Postby AltruismIsDead » Fri Dec 30, 2011 5:25 am

Good Tutorial Sonic! +1

Then of course there are still the bugs we talked about in the chat, but I am sure one of the gE programming wizards around here will post up a reply on how to figure that out :)
User avatar
AltruismIsDead
 
Posts: 38
Joined: Mon Sep 19, 2011 10:55 am
Location: Milwaukee, WI
Score: 6 Give a positive score

Re: Tutorial: Creating a timer

Postby phyzix5761 » Fri Dec 30, 2011 5:36 am

For the issue you are having where the seconds display as a single digit when smaller than 10 just add this


Code: Select all
if(VirtSec<10)
{
    sprintf(text, "%i:0%i", VirtMin, VirtSec);
}
else
{
   sprintf(text, "%i:%i", VirtMin, VirtSec);
}
phyzix5761
 
Posts: 262
Joined: Sun Feb 27, 2011 4:28 am
Score: 17 Give a positive score

Re: Tutorial: Creating a timer

Postby skydereign » Fri Dec 30, 2011 8:20 am

Or do this.
Code: Select all
sprintf(text, "%d:%02d", VirtMin, VirtSec);

You should read up on sprintf, it is very handy and can do pretty much any formatting you would use normally. In this case the 02 before the %d makes it so there are at least space for two digits, and the 0 itself makes the extra space filled in with 0. So, 2 would look like 02 while 12 would just look like 12.
User avatar
skydereign
 
Posts: 3453
Joined: Mon Jul 28, 2008 8:29 am
Score: 570 Give a positive score

Re: Tutorial: Creating a timer

Postby SuperSonic » Fri Dec 30, 2011 1:54 pm

Thank you for your replies everyone :D

@Phyzix: Thanks for your code. To be honest, I was actually going to use that method to solve the problem

@Skydereign: Thank you too but I'm quite sure how that code is supposed to work. When I use it, it displays things like 10:020 and 9:0259. Am I doing something wrong? :?
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1444
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial: Creating a timer

Postby Game A Gogo » Fri Dec 30, 2011 2:21 pm

skydereign wrote:Or do this.
Code: Select all
sprintf(text, "%d:02%d", VirtMin, VirtSec);

You should read up on sprintf, it is very handy and can do pretty much any formatting you would use normally. In this case the 02 before the %d makes it so there are at least space for two digits, and the 0 itself makes the extra space filled in with 0. So, 2 would look like 02 while 12 would just look like 12.

he means
Code: Select all
sprintf(text,"%d:%02d",VirtMin, VirtSec);
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3464
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Tutorial: Creating a timer

Postby SuperSonic » Fri Dec 30, 2011 2:37 pm

Wow It works! Thanks guys :D
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1444
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial: Creating a timer

Postby SuperSonic » Mon Jan 16, 2012 11:39 pm

Sorry for bumping but could this get added to the tutorials list? :roll:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1444
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Tutorial: Creating a timer

Postby skydereign » Mon Jan 16, 2012 11:48 pm

It's been added. And for future reference, if you create an account on the site, you can add to it yourself.
User avatar
skydereign
 
Posts: 3453
Joined: Mon Jul 28, 2008 8:29 am
Score: 570 Give a positive score

Re: Tutorial: Creating a timer

Postby SuperSonic » Tue Jan 17, 2012 3:08 am

Thank you sky :D
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1444
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 0 guests

cron