Page 1 of 1

Timer problem

Posted: Sat May 02, 2015 5:44 pm
by ian363
I've set up a button which activates a teleporter and a timer. The timer deactivates the teleporter after 5 seconds. The problem is that the teleporter sometimes turns off after 5 sec or 3 sec or 1 sec, etc. Makes testing puzzle difficult. Any ideas? thanks Ian.

Re: Timer problem

Posted: Sat May 02, 2015 5:57 pm
by JohnWordsworth
One problem with timers is that they are only intended to fire at the correct interval when the user is on the same level as them - other timers update less often so that they are more gentle on system resources.

I don't know exactly what you are doing at the moment, but I can see a couple of options to solve your problem.

1. The easiest is probably just to use the "delayedCall" function, which will call a function on a script after a fixed amount of time. This means you can do away with the timer object altogether and do something like this...

Code: Select all

kTeleporterTime = 5.0

function startPuzzle()
  -- Activate Teleporter
  delayedCall(self.go.id, kTeleporterTime, "onTimeOut")
end

function onTimeOut()
  -- Deactivate Teleporter
end
2. If you have other uses for the timer that ticks during the puzzle - then you can actually check the time precisely using another function. This would allow you to set the timer interval to 0.1 say (have the timer update 10 times a second, or fewer times if the user is away from the level) and just check that the correct amount of time has passed since the puzzle started. This wouldn't always be exactly 5 seconds, but would be close.

Code: Select all

puzzleStartTime = 0
kTeleporterTime = 5.0

function startPuzzle()
  -- Activate Teleporter
  puzzleStartTime = Time.currentTime();
end

function onTimerTick()
  if ((Time.currentTime() - puzzleStartTime) > kTeleporterTime) then
    -- Deactivate Teleporter
    -- Deactivate Timer
  end
end
Edit: Where possible, it is good to take out magic numbers / constants and store them as variables. As such, I have taken the 5.0 out and made it clear what this number is. :)

Re: Timer problem

Posted: Sat May 02, 2015 6:14 pm
by ian363
[quote="JohnWordsworth"]One problem with timers is that they are only intended to fire at the correct interval when the user is on the same level as them - other timers update less often so that they are more gentle on system resources.


Thanks for the reply. I should have said I'm a beginner and haven't done scripting. I was just trying to use the tools available. However I think I understand the first script. I'm in one room, on one level, with a button, a timer and a teleport. But the teleporter does not always stay on for the time I set. I just wondered if I had missed something. Thanks Ian.

Re: Timer problem

Posted: Sat May 02, 2015 7:04 pm
by JohnWordsworth
Ahh, my apologies - I am a programmer at heart and so just jump straight to scripts for anything beyond "button opens door" etc. Lol.

I assume you have the following...

1. A teleporter.
2. A timer that "deactivates" the teleporter after 5 seconds.
3. A pressure plate that activates both the teleporter and the timer.

So, you need to make sure that the timer has the following settings...

triggerOnStart: Off - You don't want the timer to do anything to start with, only when it is activated by the pressure plate.

disableSelf: On - The timer has to turn itself off after firing once, otherwise it will fire EVERY 5 seconds forever, and will attempt to turn off the teleporter every 5 seconds (even if it is already turned off).

I expect that the timer is running continually (for one of the above 2 reasons) which means that it is trying to turn off the teleporter every 5 seconds over and over (regardless of when you activate the teleporter). So, using the above settings should fix your issue and make it always take 5 seconds :).

Re: Timer problem

Posted: Sat May 02, 2015 7:35 pm
by ian363
JohnWordsworth wrote:Ahh, my apologies - I am a programmer at heart and so just jump straight to scripts for anything beyond "button opens door" etc. Lol.

I assume you have the following...

1. A teleporter.
2. A timer that "deactivates" the teleporter after 5 seconds.
3. A pressure plate that activates both the teleporter and the timer.

So, you need to make sure that the timer has the following settings...

triggerOnStart: Off - You don't want the timer to do anything to start with, only when it is activated by the pressure plate.

disableSelf: On - The timer has to turn itself off after firing once, otherwise it will fire EVERY 5 seconds forever, and will attempt to turn off the teleporter every 5 seconds (even if it is already turned off).

I expect that the timer is running continually (for one of the above 2 reasons) which means that it is trying to turn off the teleporter every 5 seconds over and over (regardless of when you activate the teleporter). So, using the above settings should fix your issue and make it always take 5 seconds :).
You are a star. disableself on, did it. Thank you for your time and effort. Ian