Which one is better? [OnPlayerUpdate vs SetTimer]
#1

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?
Reply
#2

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
Reply
#3

In performance/optimization?
Reply
#4

Maybe I still need to know this..
Reply
#5

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

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).
Reply
#7

i prefer use an SetTimer
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)