28.08.2010, 15:07
Hello
This is a english version of my polish tutorial "Wszystko o Timerach"
What is a timer?
Timer is a, so if period of time, after which will be called the callback of your choice. We can choose how long it is to be called the callback, or if the timer is to be repeated, after a certain period of time is ticking again, or just stop ticking. You can also set the parameters of the function, which will be called.
Functions
Timers has been controled by three basic functions. SetTimer, KillTimer and SetTimerEx.
SetTimer
This function called a callback, but not specify its parameters. Have three arguments:
KillTimer
This function, removed previously created by us timer. Have one argument, ID of timer, which to be removed.
Example:
This is only example and is not very applicable.
SetTimerEx
I recommend this function only for advanced programmers. Called a callback, and specify its parameters. May have infinitely many arguments.
The following characters are presented here would be to insert.
SetTimerEx returns ID of created timer.
Example:
This is only example and is not very applicable.
I hope this tutorial will help many people, I tried to explain everything as best as I can.
Yours, Minokon
P.S
Sorry, my english is very bad
This is a english version of my polish tutorial "Wszystko o Timerach"
What is a timer?
Timer is a, so if period of time, after which will be called the callback of your choice. We can choose how long it is to be called the callback, or if the timer is to be repeated, after a certain period of time is ticking again, or just stop ticking. You can also set the parameters of the function, which will be called.
Functions
Timers has been controled by three basic functions. SetTimer, KillTimer and SetTimerEx.
SetTimer
This function called a callback, but not specify its parameters. Have three arguments:
- funcname[] - a string, which is a name of callback.
- interval - number of miliseconds, after which the callback to be called. (1000ms = 1s)
- repeating - whether the timer is to be repeated.
KillTimer
This function, removed previously created by us timer. Have one argument, ID of timer, which to be removed.
Example:
pawn Code:
#include <a_samp>
new Timer; //variable, which will store timer ID
forward function(); //example function
main() return 0;
public OnGameModeInit()
{
Timer = SetTimer("function", 10000, true); //called "function" every ten seconds
return 1;
}
public OnGameModeExit()
{
KillTimer(Timer); //removes created by us timer
return 1;
}
public function()
{
print("10 seconds elapsed.");
return 1;
}
SetTimerEx
I recommend this function only for advanced programmers. Called a callback, and specify its parameters. May have infinitely many arguments.
- funcname[] - a string, which is a name of callback.
- interval - number of miliseconds, after which the callback to be called. (1000ms = 1s)
- repeating - whether the timer is to be repeated.
- format[] - types of parameters.
- {Float,_}:... - callback parameters.
The following characters are presented here would be to insert.
- c - single character
- d, i - number (integer)
- f - number of type Float
- s - string
SetTimerEx returns ID of created timer.
Example:
pawn Code:
#include <a_samp>
forward function(playerid, msg[]);
main() return 0;
public OnPlayerSpawn(playerid)
{
SetTimerEx("function", 10000, false, "ds", playerid, "SA-MP"); //called "function" when 10 seconds elapsed
return 1;
}
public function(playerid, msg[])
{
new str[70];
format(str, sizeof str, "You are been spawned 10 seconds ago. A message for you: %s", msg);
SendClientMessage(playerid, 0xAFAFAFAA, str);
return 1;
}
I hope this tutorial will help many people, I tried to explain everything as best as I can.
Yours, Minokon
P.S
Sorry, my english is very bad