SA-MP Forums Archive
Which one is better? [OnPlayerUpdate vs SetTimer] - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Which one is better? [OnPlayerUpdate vs SetTimer] (/showthread.php?tid=495625)



Which one is better? [OnPlayerUpdate vs SetTimer] - newbienoob - 18.02.2014

pawn Код:
new tick;
public OnPlayerUpdate(playerid)
{
    if(GetTickCount() - tick > 1000)
    {
        tick = GetTickCount();
        SendClientMessage(playerid, -1, "1 second passed");
    }
    return 1;
}

//or

//gamemodeinit
timer = SetTimer("onesec", 1000, true);


forward onesec();
public onesec()
{
    SendClientMessageToAll( -1, "1 second passed");
    return 1;
}
Out of curiosity. Which way is better?


Re: Which one is better? [OnPlayerUpdate vs SetTimer] - RajatPawar - 18.02.2014

Timers are somewhat inaccurate but since you are counting seconds then the offset inaccuracies and gettickcount would compensate for each other and still ogve correct stuff. IMHO, using timers (native or y_) - checking code run only once a second whereas you would run the same checking code (has one second passed) repeatedly under OPU which runs a lot of times per second. No TLDR version


Re: Which one is better? [OnPlayerUpdate vs SetTimer] - newbienoob - 18.02.2014

In performance/optimization?


Re: Which one is better? [OnPlayerUpdate vs SetTimer] - newbienoob - 22.02.2014

Maybe I still need to know this..


Re: Which one is better? [OnPlayerUpdate vs SetTimer] - cessil - 22.02.2014

each is better in its own way, it depends on the situation


Re: Which one is better? [OnPlayerUpdate vs SetTimer] - Vince - 22.02.2014

I only use OnPlayerUpdate as an entry point for custom callbacks. Generally, when you know something has to be done, use a timer (e.g. anti-cheat timer). When you don't know, use OnPlayerUpdate (e.g. a player changing a weapon).


Re: Which one is better? [OnPlayerUpdate vs SetTimer] - PT - 22.02.2014

i prefer use an SetTimer


Re: Which one is better? [OnPlayerUpdate vs SetTimer] - ColeMiner - 22.02.2014

Quote:
Originally Posted by cessil
Посмотреть сообщение
each is better in its own way, it depends on the situation
^ This. If one was better than the other, why would we have both? If you only want to run something at a set interval, use a timer - using GetTickCount in OnPlayerUpdate is almost exactly what timers do anyway, so you are just duplicating effort. If you want something to happen as often as possible when a player's data changes, use OnPlayerUpdate.

Asking about "performance" and "optimisation" is the wrong thing to ask because I guarantee that any trivial differences between the two will be meaningless compared to the code you are running within the functions.