SA-MP Forums Archive
[Tutorial] Creating "timer-less" command timers. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Creating "timer-less" command timers. (/showthread.php?tid=344172)



Creating "timer-less" command timers. - warcodes_ - 20.05.2012

Introduction:

I have decided to start making tutorials on this forum and thought it would be best to start with something simple, yet useful to anybody.

Today i am going to show you how to make command timers with "tickcount()" using two simple steps, eliminating the need for actual timers.
(Please forgive me if this tutorial is already covered, i did not find any during my search.)


Step 1 - Creating the timer data variable/s:

I am going to show you how to do this using an enum so it will be alot easier for you to add more command timers in future, and it will be alot better then making an individual player variable for each command.

pawn Code:
enum timer_data//creating the enum
{
    CMD_yourcommand//creating the data value for the command
}
new TimerInfo[MAX_PLAYERS][timer_data];//creating the player variable
Step 2 - Creating the timer/s for your command/s:

Now basically, all we have to do is set the player variable for the command to the tickcount() after the command is used, then when the command is used again, check if the tickcount() minus w/e time we restrict is less then the player variable(tickcount()), if so they have used the command within the restricted time frame and we simply tell them "Please wait before using this command again.", simple or what?

pawn Code:
//under the command you want to add a timer to
{
    new time = tickcount() - 60000;//we set our new variable(time) to the tickcount() minus 1 minute(Example time frame)
    if(time < TimerInfo[playerid][CMD_yourcommand]) return SendClientMessage(playerid, -1, "Please wait before using this command again.");//if our new variable(explained above) is less then our player variable(tickcount()), the server has not lapsed the amount of the time frame, so tell them to wait.
    //else it has lapsed meaning the player has waited the amount of the time frame, let them continue with the command
    TimerInfo[playerid][CMD_yourcommand] = tickcount();//and set there player variable to the new tickcount()
    //command code starts here...
    return 1;
}
And it is as simple as that, i hope this tutorial helps you understand there is always a better way to do something.




Re: Creating "timer-less" command timers. - iggy1 - 20.05.2012

EDIT: tickcount is safe if you restart your samp server within 24 days. GetTickCount is not. I thought this tut was GetTickCount TBH i never know tickcount() existed. Sorry about that warcodes_


Re: Creating "timer-less" command timers. - Hiddos - 20.05.2012

Quote:
Originally Posted by iggy1
View Post
Had to switch all code to gettime().
It's not like rebooting your server every 3 weeks saves you a lot of trouble, plus still allowing you to work with milliseconds.


Re: Creating "timer-less" command timers. - iggy1 - 20.05.2012

Ignore me.


Re: Creating "timer-less" command timers. - Hiddos - 20.05.2012

Ah, I indeed misunderstood your post a bit. I thought you could just restart the server, but it doesn't solve any of your problems :\.


Re: Creating "timer-less" command timers. - warcodes_ - 21.05.2012

1. I am not using GetTickCount() in the tutorial, yet i am using tickcount()
2. If your worried about your server exceeding 24 days(Without a restart), you have serious issues.


Re: Creating "timer-less" command timers. - Niko_boy - 21.05.2012

hmm i too need suck tutorial cause i never worked with gettime stuff .. always using that timers
so you Use TickCount() , is it really safe or not ? finaly tell so i can implement and try this tutorial as /script :>
]ABove they mean GetTickCount isnt saffer hmm , do ur opinion same about TickCount , huh iggy?:P


Re: Creating "timer-less" command timers. - Lorenc_ - 21.05.2012

Quote:
Originally Posted by iggy1
View Post
Its not safe to use GetTickCount for stuff like this, i know this first hand. It bugs if the server isn't restarted within 24 days. And i don't mean samp server i mean the machine it is running on.

GetTickCount wraps to -2,147,483,647 when it gets past 2,147,483,647.

gettime() is safer to use.

I would advise nobody to use the GetTickCount method.

Kingj servers doesn't always restart within 24 days, that's how i know. One day all commands (that had anti abuse) stopped working, i printed GetTickCount and it was a negative value. Had to switch all code to gettime().

Just saying this in the hopes of saving someone time.
Both ways are fine, everyone should at least restart their server once per week sometimes two but that's when I'm not able to make a update for my server. Anyway, you are right. I'll convert soon.

I use gettickcount for most anti-spamming, gettime for expiration of things.


Re: Creating "timer-less" command timers. - Shadow_ - 21.05.2012

Quote:
Originally Posted by warcodes_
View Post
1. I am not using GetTickCount() in the tutorial, yet i am using tickcount()
2. If your worried about your server exceeding 24 days(Without a restart), you have serious issues.
Actually you have serious issues, he is not saying that his personal server isnt going to restart. He is saying that the MAIN Hosted server isnt going to restart, if you could read/understand english you would know that.


Re: Creating "timer-less" command timers. - warcodes_ - 21.05.2012

Quote:
Originally Posted by Shadow_
View Post
Actually you have serious issues, he is not saying that his personal server isnt going to restart. He is saying that the MAIN Hosted server isnt going to restart, if you could read/understand english you would know that.
HE obviously RUNS his SERVER, meaning he has to restart it...which should be like the person before you stated, once a week or twice MAX.

That's a maximum of 14 days meaning he should then be 864 000 000 milliseconds away from the function even tripping out.