07.07.2013, 22:00
(
Last edited by bensmart469; 01/04/2015 at 10:37 AM.
)
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:
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:
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.
Now, lets make the timer, and merge our other timers.
You can easily do this for settimerex too e.g:
If you want the timers to occur more than one time, you can simply do this:
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.
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
}
PHP Code:
new timer1 = 0, timer2 = 0, timer3 = 0;
PHP Code:
SetTimer("Global",1000,1); // repeating timer of "Global"
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...
}
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
}
}
}
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...
}
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.