Forum
CS2D Scripts Do timers ever automatically freeDo timers ever automatically free
4 replies 1
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
2
timer(1000, 10, function() msg('fx') end) timer(1500, 5, function() msg('fy') end)
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
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
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
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.
edited 1×, last 12.08.17 02:49:32 pm
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.
1