[Script] mk's timer library (for lua coders) |
Mynameismk « Citoyen » 1532388000000
| 3 | ||
mk's timer library Well, since the module's api does not allow non-module team people to use timers. I decided to create a small helper that handles timers. To make it clear, this is made for developers and it's not for players (sorry if I did not make that clear). there are a few things to note: - These timers aren't accurate. - The library has limitations. - You have to start a new round before using it (tfm.exec.newGame). - I tested pretty little, it worked fine, but please report any problems. - Also, it's somewhat similar to the Module API's timer system and also uses camelCase Make sure to check documentation for usage. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 -- A small timer library since module timers are unaccessible by regulars 1 2 Timers={hasPassed={},amount={},start={},current=0}function newTimer(a,b)if Timers.hasPassed[a]==nil then print("Created timer (ID : "..a..")")table.insert(Timers.hasPassed,a,false)table.insert(Timers.start,a,Timers.current)table.insert(Timers.amount,a,b)return a end end;function removeTimer(a)print("Removed timer (ID : "..a..")")Timers.hasPassed[a]=nil;Timers.amount[a]=nil;Timers.start[a]=nil end;function resetTimer(a,b)if b~=nil then Timers.amount[a]=b end;Timers.start[a]=Timers.current;Timers.hasPassed[a]=false end;function processTimers(c,d)Timers.current=Timers.current+500;for e,f in pairs(Timers.hasPassed)do if Timers.current>Timers.start[e]+Timers.amount[e]and not Timers.hasPassed[e]then Timers.hasPassed[e]=true end end end How to use: 1. Copy all the code in the Code section and paste it to the top of your lua code. (If you don't want to examine the code, you can use the minified version which only takes one line (useful for people that don't use an IDE)) 2. Add the proccessTimers(c,r) at the loop event (eventLoop) and pass the two arguments "current time" and "time remaining" Example: 1 2 3 4 -- End of Timers Creating a timer : To create a timer, you use the function newTimer, the function requires two arguments, ID of timer and the duration of the timer. newTimer function also returns the ID specified, so if you use the number 5 as an ID, the function returns 5. Duration is specified in milliseconds. (1 second = 1000 millisecond) Timer has to be atleast 500ms (0.5s in duration) due to the TFM limitations. Example : 1 2 3 4 5 6 -- Making a timer with duration of 10 seconds and ID of 5 If you want to check if a timer has passed, you use Timers.hasPassed[ID]. It will either return true or false. Example: 1 2 3 4 5 6 7 8 9 10 11 12 -- Always restart the round at the start of your code, else the library will malfunction To reset a timer, it's pretty simple. You just use the function resetTimer, and it takes one argument which is ID of the timer. But if you did not notice resetTimer has an optional argument which is amount (duration of the timer). If you don't define a second argument it will just reset the timer, but if you do it will reset it and change the duration of the timer. Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -- Always restart the round at the start of your code, else the library will malfunction I won't take time to explain this part, to remove a timer you simply use removeTimer and you only pass ID as an argument. After removing a timer, it will just return nil after you try to call it. Don't worry *Timer functions check if it's a nil but Timers.hasPassed does not. nil checking no longer exists due to lua interepting nil as false. Example: 1 removeTimer(myTimer) If you did not notice, there is a table called Timers, this table contains all variables used by the library. You should never change any variable in this table. KEEP IT READ ONLY Instead, you can use it to check the duration of a timer using "Timers.amount[ID]" and the current time of the clock (since the script got executed using "Timers.current". Other than that, there is no use of that table but for the library. Sorry, the reference is yet to be made. Dernière modification le 1532455020000 |
Bolodefchoco « Sénateur » 1532399160000
| 0 | ||
You could at least indent the code :P Nice. |
Mynameismk « Citoyen » 1532442720000
| 0 | ||
Bolodefchoco a dit : I've indented the code as you wanted, and I've added a minified version for the ones who do not need to examine the code. |
Mynameismk « Citoyen » 1538835960000
| 0 | ||
bump. also do you guys think this will ever be helpful for anyone? |
Babyse2 « Citoyen » 1557108900000
| 0 | ||
nice!! |
0 | ||
Seems really helpful! Going to use this in future projects |