[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
#2

Very nicely explained,good job!

Just one mistake:
pawn Code:
Timer = SetTimer("funkcja", 10000, true)
I am guessing funkcja should be function?

PS: So this is where I made my 1337th post..
Reply
#3

Haha ... You can see that converted from the Polish, because I still "function" instead of "function" in the show, not only the example above.

/ / Sorry for bad English.
Reply
#4

[XST]O_x, thanks xD I don't see that
FakeMan, yes, it's converted from polish
Reply
#5

It very easy to read, but some feedback:
- this isn't much more than you can find in the wiki.
You said "ALL about timers" so you should also tell the reader about this:
https://sampforum.blast.hk/showthread.php?tid=61322
Sorry, I don't want too sound rigorous.
Reply
#6

nice tut.
Reply
#7

I needed this 15 months ago for SetTimerEx xD - whatever I know it long enough now.
It's nice and clean. I think, if I didn't knew anything about it yet, I would understand now.

Thumb up ^^
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)