Timer problem
Timer problem
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.
- JohnWordsworth
- Posts: 1397
- Joined: Fri Sep 14, 2012 4:19 pm
- Location: Devon, United Kingdom
- Contact:
Re: Timer problem
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...
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.
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. 
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
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
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Re: Timer problem
[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.
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.
- JohnWordsworth
- Posts: 1397
- Joined: Fri Sep 14, 2012 4:19 pm
- Location: Devon, United Kingdom
- Contact:
Re: Timer problem
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
.
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
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Re: Timer problem
You are a star. disableself on, did it. Thank you for your time and effort. IanJohnWordsworth 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.