×

Langue

Fermer
Atelier 801
  • Forums
  • Dev Tracker
  • Connexion
    • English Français
      Português do Brasil Español
      Türkçe Polski
      Magyar Română
      العربية Skandinavisk
      Nederlands Deutsch
      Bahasa Indonesia Русский
      中文 Filipino
      Lietuvių kalba 日本語
      Suomi עברית
      Italiano Česky
      Hrvatski Slovensky
      Български Latviešu
      Estonian
  • Langue
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [Script] mk's timer library (for lua coders)
[Script] mk's timer library (for lua coders)
Mynameismk
« Citoyen »
1532388000000
    • Mynameismk#0000
    • Profil
    • Derniers messages
    • Tribu
#1
  3
  • Introduction
  • Code
  • Minified Code
  • Documentation
  • Reference

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
-- library made by "mk" < report any problems in the topic >
Timers = {hasPassed={},amount={},start={},current=0}
function newTimer(id,amount)
-- Timer's amount should be atleast 500 ms (0.5s) due to TFM limitation
if Timers.hasPassed[id] == nil then
print("Created timer (ID : ".. id ..")")
table.insert(Timers.hasPassed,id,false)
table.insert(Timers.start,id,Timers.current)
table.insert(Timers.amount,id,amount)
return id
end
end
function removeTimer(id)
-- Remove specified timer by ID
print("Removed timer (ID : ".. id ..")")
Timers.hasPassed[id] = nil
Timers.amount[id] = nil
Timers.start[id] = nil
end
function resetTimer(id,amount)
-- Resets a timer (if an amount was specified then Timer's duration changes)
if amount ~= nil then
Timers.amount[id] = amount
end
Timers.start[id] = Timers.current
Timers.hasPassed[id] = false
end
function processTimers(c,r)
-- Processes the timers
-- I've changed the way current time works to emulate a clock, let's hope it does not mess up
Timers.current = Timers.current+500
for x,k in pairs(Timers.hasPassed) do
if Timers.current > (Timers.start[x]+Timers.amount[x]) and not Timers.hasPassed[x] then
Timers.hasPassed[x] = true
end
end
end
-- End of Timers
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
-- End of Timers
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
function eventLoop(current,remaining)
processTimers(current,remaining)
end
Now you can successfully use 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
mytimer = newTimer(5,10000)
print(mytimer)
-- Prints 5
-- Now when you want to use the ID, you can either call the mytimer variable
-- or just use 5
Using a timer:
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
tfm.exec.newGame()
-- Create a timer with the duration of 10 seconds and the ID of 1
myTimer = newTimer(1,10000)
function eventLoop(current,remaining)
-- Check if the timer has passed
if Timers.hasPassed[myTimer] then
print("the timer has passed!")
end
-- Process the timers library
processTimers(current,remaining)
end
Resetting a timer:
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
tfm.exec.newGame()
-- Create a timer with the duration of 10 seconds and the ID of 1
myTimer = newTimer(1,10000)
function eventLoop(current,remaining)
-- Check if the timer has passed
if Timers.hasPassed[myTimer] then
print("the timer has passed!")
-- Resets the timer and changes the duration of it from 10 seconds to 20 seconds
resetTimer(myTimer,20000)
-- Now this will keep on looping
end
-- Process the timers library
processTimers(current,remaining)
end
Removing a timer:
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)
The timers table:
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.
Please, report any problems since this is experimental.


Dernière modification le 1532455020000
Bolodefchoco
« Sénateur »
1532399160000
    • Bolodefchoco#0095
    • Profil
    • Derniers messages
    • Tribu
#2
  0
You could at least indent the code :P

Nice.
Mynameismk
« Citoyen »
1532442720000
    • Mynameismk#0000
    • Profil
    • Derniers messages
    • Tribu
#3
  0
Bolodefchoco a dit :
You could at least indent the code :P

Nice.

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
    • Mynameismk#0000
    • Profil
    • Derniers messages
    • Tribu
#4
  0
bump. also do you guys think this will ever be helpful for anyone?
Babyse2
« Citoyen »
1557108900000
    • Babyse2#0000
    • Profil
    • Derniers messages
#5
  0
nice!!
Wtal
« Citoyen »
1564301280000
    • Wtal#5272
    • Profil
    • Derniers messages
    • Tribu
#6
  0
Seems really helpful! Going to use this in future projects
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • [Script] mk's timer library (for lua coders)
© Atelier801 2018

Equipe Conditions Générales d'Utilisation Politique de Confidentialité Contact

Version 1.27