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

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post 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!
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post 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!
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post 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.
Finished Dungeons - complete mods to play
User avatar
Neikun
Posts: 2457
Joined: Thu Sep 13, 2012 1:06 pm
Location: New Brunswick, Canada
Contact:

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

Post 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?
"I'm okay with being referred to as a goddess."
Community Model Request Thread
See what I'm working on right now: Neikun's Workshop
Lead Coordinator for Legends of the Northern Realms Project
  • Message me to join in!
User avatar
Brodie301
Posts: 286
Joined: Sun Apr 08, 2012 6:00 pm
Location: Mississippi, USA

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

Post by Brodie301 »

He would have done it in 10 min but forgot the delayScript entity (he he)
The FORCE is with me!!!
User avatar
Asteroth
Posts: 471
Joined: Wed Oct 24, 2012 10:41 am
Location: Eastern U.S.

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

Post 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.
I am the God of darkness and corruption.
viewtopic.php?f=14&t=4250
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post by Komag »

Haha, you guys are funny! :lol:
Finished Dungeons - complete mods to play
User avatar
antti
Posts: 688
Joined: Thu Feb 23, 2012 1:43 pm
Location: Espoo, Finland
Contact:

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

Post 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.
Steven Seagal of gaming industry
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

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

Post by Komag »

yeah, I'm starting to think that way too, good advice :)
Finished Dungeons - complete mods to play
Post Reply