Easy Timers 
9 comments This script does nothing on it's own and is intended to be used by Lua scripters.
Update
Code has been shortened and the bug mentioned by
Flacko in this post has been fixed.
The purpose of this script is to make using cs2d's timers easier.
Can pass any number of arguments
Automatically frees itself when no longer needed
Call anonymous functions
All timers are given a unique identifier
I realized how terrible the timer function is when I was trying to delay a function call
I can't use CS2D's timer function because it only allows one parameter to be passed. I'd need a table to keep track of everything and then I'd need to free it... what a hassle.
With the timer function provided by this script all I need to do is
That will call the setammo function one time after one millisecond and then free the timer for me!
And the timer function is compatible with any scripts that expect the original function
Usage
The function is declared as
ms - The delay in milliseconds between calls of the timer function
count - How many times the timer will run before being freed
tickfunc - The function to call
Any other parameters given will be passed to `tickfunc` when it is called.
The function will return a unique identifier for the timer so that it can later be freed if necessary. This also means you can free timers that use the same function and parameter.
If you use CS2D's timer function
Using this scripts timer function
If you need to keep track of variables between function calls then pass a table to it
The only time you need to manually free a timer is when it runs indefinitely / forever or you need to end it prematurely.
Copyright
You can freely use this script for any purpose.

Code has been shortened and the bug mentioned by

The purpose of this script is to make using cs2d's timers easier.




I realized how terrible the timer function is when I was trying to delay a function call
Code:
1
2
3
4
5
2
3
4
5
addhook("drop", "drop")
function drop(id, iid, type, ain, a)
-- Won't work. CS2D will override it
setammo(iid, ain, 0)
end
function drop(id, iid, type, ain, a)
-- Won't work. CS2D will override it
setammo(iid, ain, 0)
end
I can't use CS2D's timer function because it only allows one parameter to be passed. I'd need a table to keep track of everything and then I'd need to free it... what a hassle.
With the timer function provided by this script all I need to do is
Code:
1
2
3
2
3
function drop(id, iid, type, ain, a)
timer(1, 1, setammo, iid, ain, 0)
end
timer(1, 1, setammo, iid, ain, 0)
end
That will call the setammo function one time after one millisecond and then free the timer for me!
And the timer function is compatible with any scripts that expect the original function
Code:
1
timer(1, "print", "hello", 3) -- uses cs2d's timer, as expected
Usage
The function is declared as
Code:
1
function timer(ms, count, tickfunc, ...)
ms - The delay in milliseconds between calls of the timer function
count - How many times the timer will run before being freed
tickfunc - The function to call
Any other parameters given will be passed to `tickfunc` when it is called.
The function will return a unique identifier for the timer so that it can later be freed if necessary. This also means you can free timers that use the same function and parameter.
If you use CS2D's timer function
Code:
1
2
3
4
2
3
4
timer(1000, "msg", "hey", 0)
timer(1000, "msg", "hey", 0)
-- this will free BOTH timers. you can't free only one
freetimer("msg", "hey")
timer(1000, "msg", "hey", 0)
-- this will free BOTH timers. you can't free only one
freetimer("msg", "hey")
Using this scripts timer function
Code:
1
2
3
4
2
3
4
id = timer(1000, 0, msg, "hey")
id2 = timer(1000, 0, msg, "hey")
freetimer(id)
freetimer(id2)
id2 = timer(1000, 0, msg, "hey")
freetimer(id)
freetimer(id2)
If you need to keep track of variables between function calls then pass a table to it
Code:
1
timer(1000, 3, function (args) print(args.x); args.x = args.x + 1 end, {x = 1})
The only time you need to manually free a timer is when it runs indefinitely / forever or you need to end it prematurely.
Code:
1
2
3
4
5
2
3
4
5
-- 0 means run forever
mytimer = timer(1000, 0, msg, "hello, world")
-- somewhere else in the script
freetimer(mytimer)
mytimer = timer(1000, 0, msg, "hello, world")
-- somewhere else in the script
freetimer(mytimer)
Copyright
You can freely use this script for any purpose.
edited 4×, last 07.10.17 11:55:20 pm

Comments
9 comments



Log in!
You need to log in to be able to write comments!Log in
Don't forget, there's always the `parse` command that can be called, and the argument can be a string with Lua. This allows you to do just about anything you want with the default timer function, including passing multiple arguments.
basic example:
and a more complex example calling a class method with a sub-class method (lol):
Downside is having to deal with escaping the string properly and it's admittedly a bit messy. Having a helper function is certainly easier and cleaner. I'm sure most would prefer Easy Timers lol.
Or a slightly less messy method:
Okay, so technically it's just a single string, but you can parse it in the function that is being called. I always have a helper function `string:split()` that makes this trivial.
Just some tips for anyone searching, in case it helps.
basic example:
Code:
1
timer(5000,"parse",'lua "foo = true"')
and a more complex example calling a class method with a sub-class method (lol):
Code:
1
timer(500,"parse",'lua "users['..id..']:shop().purchases['..index..']:equip()"')
Downside is having to deal with escaping the string properly and it's admittedly a bit messy. Having a helper function is certainly easier and cleaner. I'm sure most would prefer Easy Timers lol.
Or a slightly less messy method:
Code:
1
timer(1000,"my_function",val1..","..val2..","..val3)
Okay, so technically it's just a single string, but you can parse it in the function that is being called. I always have a helper function `string:split()` that makes this trivial.
Code:
1
2
3
4
5
6
7
2
3
4
5
6
7
-- Split a string into a table at the provided separation character
function string:split(sep)
local sep,words = sep or " ", {}
local pattern = string.format("([^%s]+)",sep)
self:gsub(pattern, function(c) words[#words+1] = c end)
return words
end
function string:split(sep)
local sep,words = sep or " ", {}
local pattern = string.format("([^%s]+)",sep)
self:gsub(pattern, function(c) words[#words+1] = c end)
return words
end
Just some tips for anyone searching, in case it helps.

(I could be wrong about this and freetimer is only used to stop timers prematurely. Would be nice if this was cleared up)
That's a major issue because then you need to know when to free it.
Example
Code:
1
timer(1000, "msg", "hello", 1)
When and where do you free it? You can't know when it is finished.
Of course, you could do something like this:
Code:
1
2
3
4
2
3
4
function msgandfree(msg)
msg(msg)
freetimer("msgandfree", "msg")
end
msg(msg)
freetimer("msgandfree", "msg")
end
But then for time you want to use a timer you need to define a new function (and you can only pass a single argument).
What if you want to call the same function with the same argument in multiple timers? When one of them finishes ALL of them will be freed even if they have not completed their action yet.
edited 1×, last 07.08.17 11:19:15 pm




I'm not sure if that's correct or not but I'm going with it unless someone like

That script can free it for you with one line added
Code:
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
function t2h(i)
i = tonumber(i)
t2[i].f(unpack(t2[i].p))
if t2[i].t > 0 then
t2[i] = t2[i] - 1
else
t2[i] = nil
freetimer("t2h", i)
end
end
i = tonumber(i)
t2[i].f(unpack(t2[i].p))
if t2[i].t > 0 then
t2[i] = t2[i] - 1
else
t2[i] = nil
freetimer("t2h", i)
end
end
edited 1×, last 06.08.17 07:11:37 am
Or try
EnderCrypt's
Timer2 - easy timer! (4)



Code:
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
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
-- Timer 2
function timer2(d, p, t, f) -- You can edit this to be easier to use :D
local i = #t2 + 1
t2[i] = {
p = p,
f = f,
t = t and (t == 0 and -1 or t) or 1
}
timer(d, "t2h", i, t)
return i
end
function freetimer2(i)
if i then
freetimer("t2h", i)
t2[i] = nil
else
t2 = {}
end
end
function t2h(i)
i = tonumber(i)
t2[i].f(unpack(t2[i].p))
if t2[i].t > 0 then
t2[i] = t2[i] - 1
else
t2[i] = nil
end
end
t2 = {}
function timer2(d, p, t, f) -- You can edit this to be easier to use :D
local i = #t2 + 1
t2[i] = {
p = p,
f = f,
t = t and (t == 0 and -1 or t) or 1
}
timer(d, "t2h", i, t)
return i
end
function freetimer2(i)
if i then
freetimer("t2h", i)
t2[i] = nil
else
t2 = {}
end
end
function t2h(i)
i = tonumber(i)
t2[i].f(unpack(t2[i].p))
if t2[i].t > 0 then
t2[i] = t2[i] - 1
else
t2[i] = nil
end
end
t2 = {}
edited 1×, last 06.08.17 05:26:55 am



