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

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

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

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

- 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

-Sonic