gettime() or timers.
#1

I'm using gettime() sometimes in /pay command for example.

Checks if the current time minus the var with the old time is less than 2.
pawn Код:
if(gettime() - GetIntVar(playerid, "JustPaid") < 2) return SCM(playerid, COLOR_LIGHTRED, "[SERVER]: Please wait 2 seconds before paying again. (Security Reasons)");
After the player pays:
pawn Код:
SetIntVar(playerid, "JustPaid", gettime());
Or just use a timerex?
pawn Код:
CMD:pay(playerid, params[])
{
    new pid, amount, string[256];
    if(sscanf(params,"dd",pid,amount)) return SyntaxMSG(playerid, "/pay [playerid] [amount]");
    if(pid == playerid) return SCM(playerid, -1, "You cannot pay to yourself.");
    if(gettime() - GetIntVar(playerid, "JustPaid") < 2) return SCM(playerid, COLOR_LIGHTRED, "[SERVER]: Please wait 2 seconds before paying again. (Security Reasons)");
    if(!PlayerIsOn(pid)) return NotConnectedMSG(playerid);
    if(amount > GetCash(playerid) || amount <= 0) return SCM(playerid, -1, "You don't have enough money.");
    if(amount > 1000 && GetLevel(playerid) <= 3) return SCM(playerid, -1, "Players under level '3' aren't able to pay more than 1000.");
    if(!PlayerNearPlayer(5.0, playerid,pid)) return NotNearPlayerMSG(playerid);
    SetIntVar(playerid, "JustPaid", gettime());
    GiveCash(playerid, -amount);
    GiveCash(pid, amount);
    format(string, sizeof(string), "You have sent %s[ID:%d], $%d.", GetNameWithMask(pid),pid, amount);
    SCM(playerid, COLOR_GRAD1, string);
    format(string, sizeof(string), "You have recieved $%d from %s[ID:%d].", amount, GetNameWithMask(playerid), playerid);
    SCM(pid, COLOR_GRAD1, string);
    format(string, sizeof(string), "* %s takes out some cash, and hands it to %s.", GetNameWithMask(playerid) ,GetNameWithMask(pid));
    ProxDetector(30.0, playerid, string, COLOR_PURPLE);
    return 1;  
}
So what's better timerex or gettime?!
Reply
#2

It's best to use gettime(). Both gettime() and SetTimerEx require a variable, but gettime() doesn't require you to start a timer which uses resources.
Reply
#3

Quote:
Originally Posted by ******
Посмотреть сообщение
OK, when you are using PVars, you can't then worry about the efficiency of any other bits of code!
Sarcasm? DeletePVar.
Reply
#4

****** is right about the pVars, look at a thread he made a while (2 years ftw) ago: PVars - when, and when not, to use them.

OT: the most important thing has been said, it's better to use gettime() because it doesn't require the resources SetTimerEx requires.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)