13.03.2010, 22:38
[Tutorial] SetTimer
* I decided to rewrite my tutorial, as it was all wrong before! (I was a really new scripter back then.)
What this tutorial will cover
* Explanation for the function SetTimer + example.
* Explanation for the function KillTimer + example
The function SetTimer
The function with parameters looks like this:
Код:
SetTimer(funcname[], interval, repeating)
"funcname[]" Name of the function to call as a string("name"), and it needs to be a public(I explain later how to make it public.)
"interval" time in milliseconds, e.g 1 sec will be 1000 milliseconds.
"repeating" Should it keep repeating it self or just occur once? 0 = Will only occur once | 1 = Will repeat it self.
Now that we know what the parameters is/means we need to make the timer.
As i wrote in the parameter "funcname[]" it has to be a public, which means we have to forward it.
All you have to add is "forward funcname();"
e.g
pawn Код:
forward MyTimer();
Now we need to use the function SetTimer to set the interval and if repeated or not.
This HAS to be under a public like OnGameModeInnit.
Код:
SetTimer(funcname[],interval,repeating);
pawn Код:
public OnGameModeInnit()
{
SetTimer("MyTimer",10000,0);
}
"MyTimer" - The name called as a string (" ").
10000 - Time in milliseconds (10 seconds).
0 - Non-repeating.
Now we forward and made the timer to occur once, 10 seconds after the gamemode has been started.
Now we need to make the "public" to tell the timer what it should do after those 10 seconds.
You make the public simply with "public TimerName()" - I usually make this a the bottom of my script, but it just have to be outside the other publics.
e.g
pawn Код:
public MyTimer()
e.g
pawn Код:
public MyTimer()
{
SendClientMessageToAll(0xFFFF00AA,"10 seconds has passed");
}
Because we used
Код:
SetTimer("MyTimer",10000,0);
this is how it should look like - Click me
another example with a repeating timer would be this
The little change of af "0" to a "1" and "10000" to "60000" will make this timer to send the message "1 min has passed" every single minute.
You can read the wiki page here
The function KillTimer
This is exactly what it sounds like, it kills a running timer.
The function with parameter:
Код:
KillTimer(timerid)
You do that by simply add e.g:
Код:
new timer1;
and add the name "timer1" to the "SetTimer" function like this:
pawn Код:
public OnGameModeInnit()
{
timer1 = SetTimer("MyTimer",10000,0);
}
now you will be able to use:
Код:
KillTimer(timer1);
Like the example i used with the timer keep sending the msg "1 min has passed"
you can use it like this
That would kill the timer the first time it occurs.
I hope you learn something about timers.
Regards Naxix
* The reason why "SetTimerEx" isn't included is, that it would be able to confuse new scripters. And when you learned the function SetTimer, SetTimerEx aren't that hard anymore.