> > CS2D > Scripts > Do timers ever automatically free
Forums overviewCS2D overview Scripts overviewLog in to reply

English Do timers ever automatically free

4 replies
To the start Previous 1 Next To the start

old Re: Do timers ever automatically free

User Off Offline

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:
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
edited 1×, last 09.08.17 06:26:19 pm

old Re: Do timers ever automatically free

User Off Offline

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.

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

More >

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

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

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.
edited 1×, last 12.08.17 02:49:32 pm

old Re: Do timers ever automatically free

Admin Off Offline

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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview