Page 1 of 1

[BUG] Timer:setTimerInterval(interval) is buggy! [SOLVED]

Posted: Sat Dec 08, 2012 3:32 am
by Komag
I had some problems with this today, so I did a test map

just first empty room:
one script "countScript"
one timer "countTimer"
one button to start things, linked to activate timer, that's all
timer linked to script with "activate" -> "display"

script:

Code: Select all

function countTimerSet()
  local delay = math.random(3,6)
  countTimer:setTimerInterval(delay)
  print("timer set to "..delay)
end

function display()
  print("TICK TOCK!")
  countTimerSet()
end
When I push the button the timer starts with it's default 1s time, then I see TICK TOCK in the console with the random number.

BUT THE NUMBER IS WRONG!

Sometimes it's right, but just watch a stopwatch or listen to a clock ticking in your computer room, and almost always the next TICK TOCK message is NOT after the right amount of time. Sometimes it seems to fire off almost two in a row immediately, other times it seems to take twice as long as it should, so 4 seconds become 8, or anywhere in-between

(although usually it seems to be an integer at least, the firing seems to always be on-the-second and not a fraction)

It's totally unreliable!

Re: who else notice Timer:setTimerInterval(interval) is bugg

Posted: Sat Dec 08, 2012 3:36 am
by Komag
I discovered that if I set the timer to also deactivate itself and then activate it again each time in the script, the delay will always be exactly double what it is supposed to be!

Code: Select all

function countTimerSet()
  local delay = math.random(3,6)
  countTimer:setTimerInterval(delay)
  print("timer set to "..delay)
  countTimer:activate()
end

function display()
  print("TICK TOCK!")
  countTimerSet()
end
So when I see 3 the next TICK TOCK is after 6 seconds, and when I see 5 it really takes 10. At least this way it's reliable, if reliably wrong!

--------------

EDIT - and it has nothing to do with the math.random element. If you simply do:
countTimer:setTimerInterval(3)
it will still always take 6 seconds!

Re: who else notice Timer:setTimerInterval(interval) is bugg

Posted: Sat Dec 08, 2012 3:51 am
by Komag
I found a workaround - have a tiny delay timer as an in-between

"tinyDelayTimer" set to 0.001s

- button starts countTimer
- countTimer triggers countScript.display() and tinyDelayTimer (and deactivates itself)
- tinyDelayTimer triggers countScript.countTimerSet() (and deactivates itself)
countScript:

Code: Select all

function countTimerSet()
  local delay = math.random(3,6)
  countTimer:setTimerInterval(delay)
  print("timer set to "..delay)
  countTimer:activate()
end

function display()
  print("TICK TOCK!")
end
Works perfectly, fully accurate. I guess timers don't like to get their intervals changed right at the very moment they're firing off! A nice fat 0.001s delay gives them plenty of time to relax and adjust to change on their schedule.

Re: [BUG] Timer:setTimerInterval(interval) is buggy! [SOLVED

Posted: Sat Dec 08, 2012 3:59 am
by Neikun
A delay within a delay?

*LAST YEAR'S MEMES EVERYWHERE*

Good job sorting this out, though.
I think you did it in record time, too haha. 20 minutes?

Re: [BUG] Timer:setTimerInterval(interval) is buggy! [SOLVED

Posted: Sat Dec 08, 2012 4:14 am
by Brodie301
He would have done it in 10 min but forgot the delayScript entity (he he)

Re: [BUG] Timer:setTimerInterval(interval) is buggy! [SOLVED

Posted: Sat Dec 08, 2012 4:20 am
by Asteroth
BUT THE NUMBER IS WRONG!
For some lunatic reason that made me think "BUT WHO WAS WRONG NUMBER?".
I've gotta calm down and smack myself.

Re: [BUG] Timer:setTimerInterval(interval) is buggy! [SOLVED

Posted: Sat Dec 08, 2012 4:25 am
by Komag
Haha, you guys are funny! :lol:

Re: [BUG] Timer:setTimerInterval(interval) is buggy! [SOLVED

Posted: Tue Dec 11, 2012 2:41 pm
by antti
Probably the most reliable way to do timings with varying intervals would be to just use a steady interval in the timer and connect the timer to a counter where all the randomization and such is done.

Re: [BUG] Timer:setTimerInterval(interval) is buggy! [SOLVED

Posted: Tue Dec 11, 2012 3:10 pm
by Komag
yeah, I'm starting to think that way too, good advice :)