12.10.2010, 10:36
(
Last edited by Lenny the Cup; 12/10/2010 at 11:53 AM.
)
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:
Now I put the variable inside OnPlayerUpdate, which is called lots of times each second.
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:
(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:
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!
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];
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;
}
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!
}
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
}
}
Good luck!