Files

> > CS2D > Lua Scripts > Easy Timers
Files overviewCS2D overviewLua Scripts overview

English Easy Timers >

9 comments777 b, 456 Downloads

old Easy Timers

Vehk
User Off Offline

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 user 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

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
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

1
timer(1, "print", "hello", 3) -- uses cs2d's timer, as expected

Usage
The function is declared as
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
1
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")

Using this scripts timer function
1
2
3
4
id = timer(1000, 0, msg, "hey")
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
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.

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 4×, last 07.10.17 11:55:20 pm
Approved by Yates

Download Download

777 b, 456 Downloads

Comments

9 comments
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in

old

VaiN
User Off Offline

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:
1
timer(5000,"parse",'lua "foo = true"')
and a more complex example calling a class method with a sub-class method (lol):
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:
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.
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.

old

Nekomata
User Off Offline

Very nice.
I like it!

old

_Yank
User Off Offline

@user Ajmin, @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.
I like it!

old

Vehk
User Off Offline

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

old

Ajmin
User Off Offline

Splendid!

@user _Yank: at instance,if you wanna disable it before it gets triggered.
I like it!

old

_Yank
User Off Offline

@user Vehk: Why would you free it anyways, isn't it only triggered once ?
I like it!

old

Vehk
User Off Offline

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
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

old

Waldin
User Off Offline

Or try user EnderCrypt's file cs2d Timer2 - easy timer!
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

old

_Yank
User Off Offline

nice nice
I like it!
To the start Previous 1 Next To the start