[Tutorial] All about Timers
#1

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:
  • 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.
SetTimer returns ID of created timer.

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;
}
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.
  • 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.
In format we have to specify the types of each argument, starting from the first.
The following characters are presented here would be to insert.
  • c - single character
  • d, i - number (integer)
  • f - number of type Float
  • s - string
There are more, but I put only the basics.

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;
}
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
Reply


Messages In This Thread
All about Timers - by Minokon - 28.08.2010, 15:07
Re: All about Timers - by [XST]O_x - 28.08.2010, 15:12
Re: All about Timers - by FakeMan - 28.08.2010, 15:13
Re: All about Timers - by Minokon - 28.08.2010, 15:17
Re: All about Timers - by Remis - 29.08.2010, 13:22
Re: All about Timers - by olabv - 15.12.2010, 21:16
Re: All about Timers - by Kwarde - 15.12.2010, 21:18

Forum Jump:


Users browsing this thread: 2 Guest(s)