14.04.2015, 17:07
y_timers
Introduction
This include allows you to quickly and easily define timer functions, that is functions which are to be called after a given time. The library provides two systems: tasks, which are functions that run all the time in the background (for example streamers); and timers, which are functions you can start, stop, and call on a delay at will.
Code:
#include <a_samp> forward RepeatingTimer(); forward DelayedTimer(playerid); public RepeatingTimer() { printf("Called every N seconds."); } public DelayedTimer(playerid) { printf("May be called after N seconds"); } main() { SetTimer("RepeatingTimer", 1000, 1); SetTimerEx("DelayedTimer", 500, 0, "i", 42); }
Code:
#include <YSI\y_timers> task RepeatingTimer[1000]() { printf("Called every 1 seconds."); } timer DelayedTimer[500](playerid) { printf("May be called after 0.5 seconds"); } main() { defer DelayedTimer(42); }
- Calling conventions are defined at the function itself - when you write a function you can define how it is called.
- Times are defined with functions, so you don't have half of a function's information in one place, and more elsewhere.
- Tasks with the same delays are automatically balanced to not occur at the same time. For example if you have three with a one second period, they will be called a third of a second apart.
- Changing a function to a timer function no longer requires you to modify all the calls to it.
- The compiler can check function parameters and function names are correct.
This library is a part of YSI, which can be found here. Keep your eye on that topic and your server log for updates.
YSI Download Topic
Tasks
Tasks are functions that are called constantly at a given period. Defining these is now VERY simple:
Code:
#include <YSI\y_timers> task RepeatingFunction1[1000]() { printf("Called every second, but not at the same time as RepeatingFunction2."); } task RepeatingFunction2[1000]() { printf("Called every second, but not at the same time as RepeatingFunction1."); } ptask RepeatingFunction3[500](playerid) { printf("Called every 500ms PER PLAYER (balanced internally)."); }
Code:
task FUNCTION_NAME[DELAY]() { CODE(); } ptask FUNCTION_NAME[DELAY](playerid) { CODE(); }
* Note that internally it might not be seven REAL timers, it just means that the function will be called seven times every half second, the system load-balances internally.
- Calling
Code:
#include <YSI\y_timers> task RepeatingFunction[1000]() { printf("Called every second, and when called explicitly."); } main() { RepeatingFunction(); }
- Notes
The balancing algorithm is not perfect. If you have one timer at 400ms and one at 700ms, they will conflict once every 2.8s. This currently only offsets timers with the same periods, however cross-period-collisions are much rarer so not an issue most of the time.
Timers
Timers (as ever) allow you to call functions after a given time, without needing to mess about with "SetTimerEx":
Code:
#include <YSI\y_timers> timer DelayedTimer[500](playerid) { printf("May be called after 0.5 seconds."); } main() { defer DelayedTimer(42); }
Code:
timer FUNCTION_NAME[DELAY](PARAMETERS) { CODE(); }
Code:
timer Timer1[6 * 4]() { } timer Timer2[something](something) { } #define TIME 1000 timer Timer3[TIME]() { }
- Calling
- defer
Code:
main() { defer DelayedFunction(42); }
- repeat
Code:
main() { repeat DelayedFunction(42); new Timer:x = repeat DelayedFunction(42); stop x; }
- Normal call
Code:
main() { DelayedFunction(42); }
- Control
- Overrides
Code:
#include <YSI\y_timers> timer DelayedFunction[1000](playerid) { printf("USUALLY called after 1 second."); } main() { // Called instantly. DelayedFunction(42); // Called in 1 second (default). defer DelayedFunction(42); // Called in 200 ms (override). defer DelayedFunction[200](42); // Called every second (default). repeat DelayedFunction(100); // Called every 7 seconds (override). repeat DelayedFunction[7 * 1000](100); }
- Arrays and Strings
Code:
#include <YSI\y_timers> // Arrays must ALWAYS be followed by their length. // ALWAYS! timer ArrayTimer[500](arr[], len) { printf("Array (%d): %d, %d, ...", len, arr[0], arr[1]); } // Strings DO NOT need to be followed by their length, but MUST have // a tag of "string:" (don't worry about tag mis-matches though). timer StringTimer[500](string:str[]) { printf("String: %s", str); } main() { new array[3] = {42, 43, 44}; defer ArrayTimer(array, sizeof (array)); defer StringTimer("Hi there"); }