27.07.2015, 00:07
(
Last edited by TwinkiDaBoss; 27/07/2015 at 01:11 PM.
)
Introduction
Hello, recently I have been introduced to Unix Timestamps which are awesome way to keep your server preety much timer less.
How does this benefit you?
Well running over lets say 50-60 timers in around 50.000 lines of code made me wonder the hell is causing the lag and such, so I decided to check out time stamps. The advantage is huge, less CPU & ram usage, 100% more accurate (no timerfix needed or such) and the best part, it cuts down the lines and everything is kept inside the command itself so you dont have bunch of callbacks to check your timers.
Coding
First if all, we want to make a function that would save gettime. What is gettime? It gets all time lapsed since Thursday, 1 January 1970 in seconds.We are saving all seconds passed since 1970 so we can later on compare it to current time
now lets make a variable that will save our actual cooldown. Later on we will use that variable to read the currenttime
add this on TOP of your script
Now we want to compare that time. How do we do that? Well we already have variable named "currenttime" thats saving all seconds passed since 1970 so all we have to do compare it to the current time and seconds we want.
Okay whats happening here, lets break it into pieces. As we said before "currenttime" is used to save our current time. We are using "MyCommandCooldown[playerid]" to check the time that we will save later when the player used the command. Now number "60" are our seconds. As in easier explanation its something like this.
We are using ">" to see if the TimeWhenWeUsedCommand is bigger than TimeNow aka if 60 seconds passed.
Now after we checked that, we will save player time when he used the command! Same way we saved the currenttime
We have saved the time when the player used the command into variable called "MyCommandCooldown" and we assigned it to the playerid.
Now we did all of that, lets format a message letting player know how much time needs to pass until he can use the command. We will create new string and format it
new string[64];
Okay so what happends here, we are using the time we made our cooldown + 60 seconds and then we are substracting it with the current time. This way we will get lets say
We used the command at 00:01:20, now its 00:01:40, there is 20 seconds difference. We are looking for "00:02:20" aka 1 minute/60 seconds. So preety much math
When it should expire - current time = (TimeWhenWeUsedCommand+1 minute/60 seconds) 00:02:20 - 00:01:40 (CurrentTime) = 40 seconds left
Credits
Huge thanks to SickAttack for helping me shitton of times and introducing me with this entire UnixTimestamp system!
Huge thanks to __ for making awesome tutorial on unix timestamps! Look at his advanced and more in depth tutorial [https://sampforum.blast.hk/showthread.php?tid=254915]
Hello, recently I have been introduced to Unix Timestamps which are awesome way to keep your server preety much timer less.
How does this benefit you?
Well running over lets say 50-60 timers in around 50.000 lines of code made me wonder the hell is causing the lag and such, so I decided to check out time stamps. The advantage is huge, less CPU & ram usage, 100% more accurate (no timerfix needed or such) and the best part, it cuts down the lines and everything is kept inside the command itself so you dont have bunch of callbacks to check your timers.
Coding
First if all, we want to make a function that would save gettime. What is gettime? It gets all time lapsed since Thursday, 1 January 1970 in seconds.We are saving all seconds passed since 1970 so we can later on compare it to current time
pawn Code:
new currenttime = gettime(); //We are saving all seconds passed since 1970 so we can later on compare it to current time
add this on TOP of your script
pawn Code:
MyCommandCooldown[MAX_PLAYERS];
pawn Code:
if(currenttime < (MyCommandCooldown[playerid] + 60))
pawn Code:
TimeNow > (TimeWhenWeUsedCommand + seconds)
Now after we checked that, we will save player time when he used the command! Same way we saved the currenttime
pawn Code:
MyCommandCooldown[playerid] = gettime();
Now we did all of that, lets format a message letting player know how much time needs to pass until he can use the command. We will create new string and format it
new string[64];
pawn Code:
format(string,sizeof(string),"Cooldown expires in %i seconds",((MyCommandCooldown[playerid] + 60) - currenttime));
We used the command at 00:01:20, now its 00:01:40, there is 20 seconds difference. We are looking for "00:02:20" aka 1 minute/60 seconds. So preety much math
When it should expire - current time = (TimeWhenWeUsedCommand+1 minute/60 seconds) 00:02:20 - 00:01:40 (CurrentTime) = 40 seconds left
Command itself
for the sake of the tutorial, ill use ZCMD created by Zeex and also ill make a command "/healme" for you so you will understand it a bit easierpawn Code:
CMD:healme(playerid,params[])
{
new
string[128], //We are creating a string where we will store our informations regarding the time left. You can change the size.
currenttime = gettime(); //We are saving the currenttime, aka seconds passed since 1970.
format(string,sizeof(string),"Command cooldown %i seconds",((HealCooldown[playerid] + 60) - currenttime));//We are formating a message that we will send to the player letting him know how many seconds are left until the time he can use the command
if(currenttime < (HealCooldown[playerid] + 60)) //If 60 seconds havent yet passed, we will send already formated message above
return SendClientMessage(playerid,COLOR_RED,string); //sending the message to the player and also using return to stop the command
SetPlayerHealth(playerid,100); //setting player health to 100, so he can be fully healed
SendClientMessage(playerid,COLOR_GREEN,"You have healed yourself to 100HP"); //letting player know he has just healed himself
HealCooldown[playerid] = gettime(); //We are saving the time when the player actually processed his command, aka when we finished everything and from this point he is on a cooldown
return true;
}
Huge thanks to SickAttack for helping me shitton of times and introducing me with this entire UnixTimestamp system!
Huge thanks to __ for making awesome tutorial on unix timestamps! Look at his advanced and more in depth tutorial [https://sampforum.blast.hk/showthread.php?tid=254915]