open.mp forum
[Pawn] YSI Logic user-incomprehension and usage - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] YSI Logic user-incomprehension and usage (/showthread.php?tid=955)



YSI Logic user-incomprehension and usage - hastlaking - 2019-12-17

Hello Community, i have this odd incomprehension with the YSI Libraries and most-partial of the documentation is missing or not yet implemented (explanation) into github repository i might say.


RE: YSI Logic user-incomprehension and usage - JustMichael - 2019-12-17

Tell us more about the parts of YSI you are having issues with?


RE: YSI Logic user-incomprehension and usage - hastlaking - 2019-12-18

mainly with timers calling them once on few public callbacks and some to initialize when needed example when player the moment the are logged in


RE: YSI Logic user-incomprehension and usage - JustMichael - 2019-12-18

Okay, so timers come in two forms, one being a regular timer and another being a repeater. A regular timer has a keyword associated with it called `defer` which means to delay a call to a function, now you can defer a function for n times, n being the number of milliseconds you wish to delay.

An example of this would go as follows:
Code:
main()
{
? ? // uses the 'defer' keyword to associated it with YSI,
? ? // and a function name and it's parameters is required afterwards?
? ? defer CheckPlayer(12);

? ? // You can override the delay set on the timer function, which is enclosed
? ? // in square brackets `[500]` by also putting square brackets with a new timer
? ? // delay inbetween?
? ? defer CheckPlayer[1000](12); // this will override the delay in the function and instead wait 1 second
? ??
}

// The 'timer' is a keyword that YSI uses to associated this function with YSI
// The number enclosed within square brackets `[500]` is the number of seconds the timer
// will be delayed for
timer CheckPlayer[500](playerid)
{
? ? // Perform some checks, or what you would normally do if this was a standard timer (SetTimer(Ex))
? ? if(IsPlayerConnected(playerid)) {
? ? ? ? printf("The player is connected");
? ? }
}

We than have repeatable timers. These timers run until manually stopped, and they can be stopped by using the `stop` keyword. To create a repeatable timer, you instead use the `repeat` keyword instead of the `defer` keyword. Here is an example:

Code:
static Timer: repeatable_timer;

main()
{
? ? // Here we are storing the timer globally, so it can later be stopped else
? ? // where in the script. Though this is only needed if the timer is to ever be stopped
? ? // timers are generally managed internally to YSI, so will be stopped when the server exits
? ? // or when the gamemode ends.
? ? repeatable_timer = repeat CheckPlayer(12);

? ? // We can also override the default delay for the function
? ? repeatable_timer = repeat CheckPlayer[1000](12); // The delay value is overridden to run every 1 second
}

// The 'timer' is a keyword that YSI uses to associated this function with YSI
// The number enclosed within square brackets `[500]` is the number of seconds the timer
// will be delayed for
timer CheckPlayer[500](playerid)
{
    // Perform some checks, or what you would normally do if this was a standard timer (SetTimer(Ex))
    if(IsPlayerConnected(playerid)) {
        printf("The player is connected");
    }

    // Lets now stop the repeatable timer just as an example
    stop repeatable_timer;
}

I hope this is enough to make you understand when to use them and how to use them. Feel free to ask anymore questions :)