English Easy Timers icon

9 comments
06.08.17 02:03:00 am
like 3 like it!
905 b, 7 Downloads
Vehk
User
Offline Off
This script does nothing on it's own and is intended to be used by Lua scripters.

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

I realized how terrible the timer function is when I was trying to delay a function call

Code:
1
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


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
function drop(id, iid, type, ain, a)
     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.

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
-- 0 means run forever
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 1×, last 07.08.17 11:22:26 pm
ok This file has been reviewed and approved by Yates (06.08.17 11:00:37 am)

Comments

9 comments
Goto Page
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in
13.08.17 11:07:46 am
Up
VaiN
User
Offline Off
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:
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
-- 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


Just some tips for anyone searching, in case it helps.
08.08.17 05:19:04 am
like I like it!
Up
Nekomata
User
Offline Off
Very nice.
06.08.17 11:38:33 pm
like I like it!
Up
_Yank
User
Offline Off
@user ajm-sh, @user Vehk: Oh I see. That's quite useful! If I were you, I'd ask DC to implement it into the game itself as it provides backwards compability.
06.08.17 11:31:38 pm
Up
Vehk
User
Offline Off
user _Yank The freetimer function will cause CS2D to release it's data about the timer. Apparently CS2D will not automatically free it for you even when the timer expires.

(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
function msgandfree(msg)
     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
06.08.17 12:33:09 pm
like I like it!
Up
ajm-sh
User
Offline Off
Splendid!

@user _Yank: at instance,if you wanna disable it before it gets triggered.
06.08.17 12:23:26 pm
like I like it!
Up
_Yank
User
Offline Off
@user Vehk: Why would you free it anyways, isn't it only triggered once ?
06.08.17 06:56:08 am
Up
Vehk
User
Offline Off
user Waldin The only thing I don't like about that script is that it doesn't free the timer for you. According to user Talented Doge the timers are not auto freed even after they expire (see: thread cs2d Do timers ever automatically free).

I'm not sure if that's correct or not but I'm going with it unless someone like user DC says otherwise.

That script can free it for you with one line added
Code:
1
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
edited 1×, last 06.08.17 07:11:37 am
06.08.17 05:15:43 am
Up
Waldin
User
Offline Off
Or try user EnderCrypt's file cs2d 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
-- 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 = {}
edited 1×, last 06.08.17 05:26:55 am
06.08.17 02:17:25 am
like I like it!
Up
_Yank
User
Offline Off
nice nice
To the start Previous 1 Next To the start