Timers -
bensmart469 - 07.07.2013
Hello, and thank you for looking at this tutorial. I will explain to you, how to use 1 timer instead of 15.
NOTE: This tutorial explains how to merge your timers into 1 timer, instead of multiple inefficient ones.
First of all, why would we want to use 1 timer?
Well, sa-mp is very inefficient with timers, so it will just cause high cpu usage. We don't want that if you have something like 100+ players playing, because it will cause them to leave, because it is getting annoying to have lag every second.
So how can i do it?
Well, you may have something like this if you have a lot of timers:
PHP Code:
forward timer1();
public timer1()
{
//code
}
forward timer2();
public timer2()
{
//code
}
forward timer3();
public timer3()
{
//code
}
This is not good, especially if they are running at the same intervals. Lets go to simply convert them all. First of all, you will need a variable for each timer. Ones that i use:
PHP Code:
new timer1 = 0, timer2 = 0, timer3 = 0;
Put this near the top of your script. Now, lets add this to a global timer. First of all, under ongamemodeinit, add something for your global timer.
PHP Code:
SetTimer("Global",1000,1); // repeating timer of "Global"
Now, lets make the timer, and merge our other timers.
PHP Code:
forward Global(); // needs to be forwarded, since it is a timer
public Global()
{
timer1 ++;
if(timer1 == 1) // 2nd number should be your timer's time / 1000, e.g. if i am running a timer at 2000 milliseconds, i will use if(timer1 == 2)
{
// timer 1 code
}
timer2 ++;
if(timer2 == 15) // read above, similar to that
{
//timer 2 code
}
// And so on...
}
You can easily do this for settimerex too e.g:
PHP Code:
new playertimer1[MAX_PLAYERS];
// The following code goes under ongamemodeinit:
SetTimer("Global",1000,1); // repeating timer of "Global"
forward Global();
public Global()
{
for(new i=0; i<MAX_PLAYERS; i++)
{
playertimer1[i] ++;
if(playertimer1[i] == 10) // Read the part for normal timers above, but the 2nd number is just the timer intervals divided by 1000
{
//code
}
}
}
If you want the timers to occur more than one time, you can simply do this:
PHP Code:
forward Global(); // needs to be forwarded, since it is a timer
public Global()
{
timer1 ++;
if(timer1 == 1) // 2nd number should be your timer's time / 1000, e.g. if i am running a timer at 2000 milliseconds, i will use if(timer1 == 2)
{
timer1 = 0; // So it can be repeated
// timer 1 code
}
timer2 ++;
if(timer2 == 15) // read above, similar to that
{
timer2 = 0; // So it can be repeated
//timer 2 code
}
// And so on...
}
Didn't like this tutorial? Post below, and i will be sure to help you. Constructive criticism is always welcomed.
PLEASE NOTE: If you have a large number of code in each one of your timers, it may not be wise to put all of these into one timer, and you could potentially split it across multiple ones, to reduce the overall lag on your system.
Re: Timers -
Goldilox - 08.07.2013
Useful! Thanks for sharing!
Re: Timers -
Ryan_Bowe - 08.07.2013
....
Re: Timers -
Eyce - 05.09.2013
Change the code to PAWN instead of PHP, other than that; nice tutorial.
Re: Timers -
dusk - 05.09.2013
pawn Code:
timer1 ++;
if(timer1 == 1) // 2nd number should be your timer's time / 1000, e.g. if i am running a timer at 2000 milliseconds, i will use if(timer1 == 2)
{
// timer 1 code
}
Will this actually work? I mean if "timer1" is a global variable, and you increment it every time the "Global" is called, wouldn't it make the code to excecute only once? As it'll never be equal to 1 again?
Re: Timers -
bensmart469 - 08.09.2013
Quote:
Originally Posted by dusk
pawn Code:
timer1 ++; if(timer1 == 1) // 2nd number should be your timer's time / 1000, e.g. if i am running a timer at 2000 milliseconds, i will use if(timer1 == 2) { // timer 1 code }
Will this actually work? I mean if "timer1" is a global variable, and you increment it every time the "Global" is called, wouldn't it make the code to excecute only once? As it'll never be equal to 1 again?
|
Apologies for the late reply,
You can set the timer1 back to 0, e.g.
pawn Code:
timer1 ++;
if(timer1 == 1) // 2nd number should be your timer's time / 1000, e.g. if i am running a timer at 2000 milliseconds, i will use if(timer1 == 2)
{
timer1 = 0; // so it can be repeated again.
// timer 1 code
}
Thanks for telling me anyway, I edited the post to mention this.