Forum

> > CS2D > Scripts > Do timers ever automatically free
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Do timers ever automatically free

4 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Re: Do timers ever automatically free

Flacko
User Off Offline

Zitieren
I don't think you need to free a timer that has expired for cleanup purposes, at least it doesn't state so in the help page: cs2d lua cmd timer.
It kinda makes sense that CS2D handles this for you. How else would you be supposed to know when to free the unused resources?
You couldn't use a second timer, but you would need a third one to free the resources from the second and so on...
It wouldn't be good design to free the timer in your callback function either, because that would make the function too specific and would require some way of counting ticks in the lua side too.

EDIT: I've just tested your custom timer functions and it seems there's a bug regarding this.
Given the following snippet:
1
2
timer(1000, 10, function() msg('fx') end)
timer(1500, 5, function() msg('fy') end)
Your timer implementation seems to free all the timers when the first one finishes. Could this also be a bug in CS2D?
Removing the oldfreetimer call in your freetimer() implementation solves this, but you won't be able to use freetimer() to free timers prematurely
1× editiert, zuletzt 09.08.17 18:26:19

alt Re: Do timers ever automatically free

Vehk
User Off Offline

Zitieren
You are probably right that CS2D does it for you. I got the idea that it didn't from the warning from cs2d lua cmd timer probably was just me being a little too paranoid

Thanks for the bug find! I did some further testing and found that it wasn't freeing all timers, but something else was wrong.

1
2
3
timer(1000, 10, function (arg) arg.x = arg.x + 1; msg('X' .. arg.x) end, {x = 0})
timer(1500, 5, function (arg) arg.x = arg.x + 1; msg('Y' .. arg.x) end, {x = 0})
timer(2000, 5, msg, "Z")

The output from above

Mehr >


After the call to freetimer the other two timers continue, but the first timer seems to end early. I was stumped until I looked at this line in timer.lua

1
timer(ms, fullname, "", count)

Got the idea that maybe the timer is freed once it's time is up but before the final call. After changing that line to this

1
timer(ms, fullname, "", 0)

I get the expected results (all output, and all 3 debug messages about the timers being freed).

That solved the problem, but what caused it in the first place?

I'll look more into that when I have time.
1× editiert, zuletzt 12.08.17 14:49:32

alt Re: Do timers ever automatically free

DC
Admin Off Offline

Zitieren
CS2D frees timers as soon as they expire. It would not make any sense to keep them because there's no way to do anything with them once they expired.

They are basically lightweight throw away thingies. Create, use, throw away.

That warning is there because you have to keep in mind that you can potentially run a lot of code frequently using timers which of course can slow down the game. The timers themselves do not add significant overhead however.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht