[Tutorial] Player-timers
#1

I have seen a lot of people using the SA-MP timers for their players, which is rather redundant if you want an efficient script. There is no point in starting a new timer just to check if they're close to something every 5 seconds, or if they're using hacks every two seconds, or anything else every x seconds.

Instead, use variables to store unix time and put them in OnPlayerUpdate. Let me demonstrate:

Issue: I want a timer to check every three seconds if a player is near XYZ.
Solution:
First I need a variable to store the timer in:
pawn Code:
new XYZCheckTimer[MAX_PLAYERS];
Now I put the variable inside OnPlayerUpdate, which is called lots of times each second.
pawn Code:
public OnPlayerUpdate(playerid)
{
    new iTime = gettime();
    if(XYZCheckTimer[playerid] < iTime)
    {
        if(IsPlayerInRangeOfPoint(playerid, range, x, y, z))
        {
            do the stuff!
            XYZCheckTimer[playerid] = iTime+3;
        }
    }
    return 1;
}
Note that this causes less CPU reduction than a timer does since it only checks for a variable in a function that is already called automatically instead of creating a new process for the server to take care of.

I can even create my own "One-second timer" here:
pawn Code:
new OneSecondTimer;

public OnPlayerUpdate(playerid)
{
    new iTime = gettime();
    if(OneSecondTimer < iTime)
    {
        OneSecond();
        OneSecondTimer = iTime;
    }
    return 1;
}

stock OneSecond()
{
    One second passed!
}
(Update)
This technique can also be applied to command restrictions (And other similar player-based restrictions). Let's say we need to make sure players can't repair their cars more often than every X seconds:
pawn Code:
new aRepairTime[MAX_PLAYERS];

public OnPlayerCommandText
{
    // ... they try to repair:
    if(aRepairTime[playerid] < gettime()) // Checks if they've waited long enough since last time (Or haven't done it at all)
    {
        // Do the repair
        aRepairTime[playerid] = gettime()+X // X = Amount of seconds you want to block them
    }
}
Please note that these timers only work if there are players online and active (Not tabbed out). OnPlayerUpdate is only called when a player sends their information to the server (Position, facing angle etc), but if they're playing this happens many times each second, as I mentioned above.

Good luck!
Reply


Messages In This Thread
Player-timers - by Lenny the Cup - 12.10.2010, 10:36
Re: Player-timers - by Rachael - 12.10.2010, 11:03
Re: Player-timers - by Lenny the Cup - 12.10.2010, 11:22
Re: Player-timers - by Rachael - 12.10.2010, 11:43
Re: Player-timers - by Lenny the Cup - 12.10.2010, 11:50
Re: Player-timers - by playbox12 - 12.10.2010, 16:18
Re: Player-timers - by Lenny the Cup - 13.10.2010, 11:13
Re: Player-timers - by Lenny the Cup - 13.10.2010, 20:23
Re: Player-timers - by Slice - 13.10.2010, 20:29
Re: Player-timers - by RoBo - 14.10.2010, 17:37

Forum Jump:


Users browsing this thread: 1 Guest(s)