SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 27/07/2015, 12:07 AM   #1
TwinkiDaBoss
Gangsta
 
Join Date: Jul 2014
Posts: 818
Reputation: 51
Default Cooldowns (No timers)

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
Code:
new currenttime = gettime(); //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
Code:
MyCommandCooldown[MAX_PLAYERS];

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.
Code:
if(currenttime < (MyCommandCooldown[playerid] + 60))
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.
Code:
TimeNow > (TimeWhenWeUsedCommand + seconds)
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
Code:
MyCommandCooldown[playerid] = gettime();
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];
Code:
format(string,sizeof(string),"Cooldown expires in %i seconds",((MyCommandCooldown[playerid] + 60) - currenttime));
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



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 easier
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;
}

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 [http://forum.sa-mp.com/showthread.php?t=254915]

Last edited by TwinkiDaBoss; 27/07/2015 at 01:11 PM.
TwinkiDaBoss is offline   Reply With Quote
Old 27/07/2015, 10:43 AM   #2
$$inSane
Gangsta
 
$$inSane's Avatar
 
Join Date: Feb 2012
Location: Behind you, you cant see me!!!
Posts: 663
Reputation: 56
Default Re: Cooldowns (No timers)

This seems to be very helpful.

I would try this method.
__________________
$$inSane is offline   Reply With Quote
Old 27/07/2015, 10:50 AM   #3
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 4,273
Reputation: 1137
Default Re: Cooldowns (No timers)

I saw one of Emmet_'s source code and noticed him making use of a function called NetStats_GetConnectedTime.

https://wiki.sa-mp.com/wiki/NetStats_GetConnectedTime

That can be a substitute for gettime(), handy if you run your server past 2038 lol. And also perhaps uses less resource (just my assumption).

It's an detailed tutorial. Nice. Only thing I can suggest is to format it nicely and make it more simple (less text).

Code:
if( currenttime < HealCooldown[ playerid ] )
    // return w.e

HealCooldown[ playerid ] = gettime( ) + 30;

I would do it like that honestly so that you don't need to be adding 30 seconds in every client message you use. You can straight off just subtract the current timestamp from thereon.
__________________
Join the best Cops And Robbers in SA-MP, today. svr.irresistiblegaming.com:7777

Lorenc_ is offline   Reply With Quote
Old 27/07/2015, 12:08 PM   #4
TwinkiDaBoss
Gangsta
 
Join Date: Jul 2014
Posts: 818
Reputation: 51
Default Re: Cooldowns (No timers)

Indeed, I overlooked that, I could do it that way also, thanks for the heads up mate.
TwinkiDaBoss is offline   Reply With Quote
Old 27/07/2015, 12:56 PM   #5
!damo!spiderman
Huge Clucker
 
!damo!spiderman's Avatar
 
Join Date: Oct 2006
Posts: 467
Reputation: 155
Default Re: Cooldowns (No timers)

Suggestion... use a font color which is actually readable on a white background for your headings
__________________
!damo!spiderman is offline   Reply With Quote
Old 27/07/2015, 01:11 PM   #6
TwinkiDaBoss
Gangsta
 
Join Date: Jul 2014
Posts: 818
Reputation: 51
Default Re: Cooldowns (No timers)

Quote:
Originally Posted by !damo!spiderman View Post
Suggestion... use a font color which is actually readable on a white background for your headings
Yeah, wanted to do that but had some problems loading the edit, ive changed it to red.
TwinkiDaBoss is offline   Reply With Quote
Old 27/07/2015, 03:38 PM   #7
Skillex333
Little Clucker
 
Join Date: Apr 2014
Location: Merc-Gaming
Posts: 10
Reputation: 0
Default Re: Cooldowns (No timers)

Very nice tutorial, I use something very similar for my PHP IRC bot.

Nicely done.
Skillex333 is offline   Reply With Quote
Old 27/07/2015, 09:20 PM   #8
TwinkiDaBoss
Gangsta
 
Join Date: Jul 2014
Posts: 818
Reputation: 51
Default Re: Cooldowns (No timers)

Quote:
Originally Posted by Skillex333 View Post
Very nice tutorial, I use something very similar for my PHP IRC bot.
Nicely done.
Thanks a lot mate, glad you like it!
TwinkiDaBoss is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timers Hansrutger Scripting Help 7 07/12/2013 06:34 PM
[Tutorial] Timers bensmart469 Tutorials 5 08/09/2013 01:55 PM
YSI Timers Skribblez Scripting Help 4 28/08/2013 07:53 AM
This timers may cause lag? Face9000 Help Archive 4 14/06/2011 09:18 PM
[Tutorial] All about Timers Minokon Tutorials 6 15/12/2010 09:18 PM


All times are GMT. The time now is 06:49 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.