Forum

> > CS2D > Scripts > How to use freetimer()?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to use freetimer()?

10 replies
To the start Previous 1 Next To the start

old How to use freetimer()?

Promaster
User Off Offline

Quote
Well... Tried to figure out how to stop a timer(function,id) when the player leave the server.
I tried to read and understand freetimer, but i have no clue how it works. Can someone give me an example how to stop a timer?

          timer(80000,"testfunction",id)
for example i have this timer activated, before the timer triggers, the player leaves. I want to stop the timer when he leaves as soon as possible. I know i do it in addhook(leave), but i want to know how i can use freetimer() to stop it

Observe, i have a timer for each player in the server, that is why i have "id" on the last in the timer of testfunction

old Re: How to use freetimer()?

Cloudy71
BANNED Off Offline

Quote
Use the Timer function and set the parameter, for example the player id. Then as you said, just simply use freetimer function with function name and the parameter you have set when you made the timer.

1
2
3
4
5
6
7
8
9
addhook("join", "hookJoin")
function hookJoin(id)
	timer(80000, "myTimer", id) -- id is the parameter for the timer to identify it while freeing.
end

addhook("leave", "hookLeave")
function hookLeave(id)
	freetimer("myTimer", id) -- Now set the parameter, so the id of the player, to identify exact timer and stop its work.
end

old Re: How to use freetimer()?

GeoB99
Moderator Off Offline

Quote
In addition to what user Cloudy71 said, using
freetimer()
without any arguments passed in the parenthesis means you're removing any timer calls as explained in the documentation. Be sure what kind of timer you want to be removed otherwise you have to got to use
freetimer()
.

P.S: "freetimer" is not a suitable title for your topic. Changed.

old Re: How to use freetimer()?

Masea
Super User Off Offline

Quote
This should be changed though. Like how we free the images. Setting an ID for each timer and freeing them as using their IDs.
Some timers such as
timer(22000,"parse","lua tween_alpha("..black_screen..",1000,0)")
may cause loads of work.

old Re: How to use freetimer()?

Promaster
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
addhook("join", "hookJoin")
function hookJoin(id)
     timer(80000, "myTimer", id) -- id is the parameter for the timer to identify it while freeing.
end

addhook("leave", "hookLeave")
function hookLeave(id)
     freetimer("", id) -- Now set the parameter, so the id of the player, to identify exact timer and stop its work.
end

Does "freetimer("", id)" removes all timer related to the id when leave?

old Re: How to use freetimer()?

Yates
Reviewer Off Offline

Quote
@user Promaster: Possibly. cs2d lua cmd freetimer states the following:

Quote
Info
Removes timers which call a certain function with a certain param. Call this function without parameters or with empty strings as parameters to remove ALL timers.

But honestly if you want to know for sure - just test it.

old Re: How to use freetimer()?

Masea
Super User Off Offline

Quote
user Promaster has written
One more question... Is there possible to stop a sv_sound2 when it plays in the middle of the sound??
There is. You have to get the duration of sound that you're going to stop. I'll show the example code and then wish you'd understand.
1
2
3
4
5
6
7
8
9
10
duration = 120 --To determine how long seconds the sound takes
c_duration = 0 --A parameter to get what time on the sound is

addhook("second","_")
function _()
	c_duration = c_duration + 1
	if c_duration == duration/2 then
		parse("sv_stopsound ...")
	end
end
Pretty much easy.

old Re: How to use freetimer()?

Cloudy71
BANNED Off Offline

Quote
I've found better idea to save some performance.
You can use function
os.time()
to get current os timestamp (in seconds).
1
2
3
4
5
6
7
8
9
10
11
12
13
mscDuration = 120 -- 120 seconds music duration
mscStart = 0 -- Variable when music was started.
function playMusic()
	mscStart = os.time()
	parse("...") -- Play the sound
end

addhook("second", "hookSecond")
function hookSecond()
	if (os.time() >= mscStart + mscDuration) then
		...
	end
end

By this you can use whatever hook you want to identify the music stop. Also you'll leave number adding variable.

But using timers is still better idea.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview