Using delta_time to make a precise timer

T

The Shatner

Guest
Here's the thing: I am adding a timer to my game, and I am having trouble with the fact that some hardware can be somewhat laggy, and the timer gets easily out of sync with the real world.
I've started working on the issue as follows:

----------------------------------------------------
CREATE EVENT:
ct_control_ini = current_time;

STEP EVENT:
ct_control = current_time - ct_control_ini;

centisecond = (ct_control*0.1);

if (centisecond >= 100) {
second ++;

ct_control_ini = current_time;
}

if (second = 60) {
minute ++;
second = 0;
}
----------------------------------------------------

That kinda worked, but I read about the legendary 'delta_time', and that it could be exactly what I need to fix the clock perfectly. However, I couldn't find any examples of delta_time used for precise time displaying, only to help fix the 'speed' of a given object. Since I am a beginner at the whole programming thing, I am having trouble to understand how can I use delta_time to fix my timer. Can anyone give me some advice?
 
R

rui.rosario

Guest
According to the documentation, delta_time "measures the time that has passed between one step and the next in microseconds (1microsecond is 1,000,000th of a second)". I think you could just use this fact and start accumulating the delta_time value in your timer.

Something like:
Code:
/// CREATE EVENT
accumulatedTime = 0;
second = 0;
minute = 0;

// STEP EVENT
accumulatedTime += delta_time;

while (accumulatedTime >= 1000000) {
    second++;
    accumulatedTime -= 1000000;
}

while (second >= 60) {
    minute++;
    second -= 60;
}
Of course this could not be very precise since you accumulate the time of the whole steps and the instance may not be the first thing created, etc.
 
Last edited by a moderator:
I

icuurd12b42

Guest
create:
next_trigger =get_timer() + 1000000;

step
var time = get_timer();
if(time>next_trigger)
{
//boom
next_trigger = time+1000000;
}

Here's the thing though. unless you are using delta time for motion, bypassing the frame rate, you should not mix real time with framerate... if, for example, you give the player 30 real seconds to reach a goal and the game lags from 60 fps to 30, then he will never reach his goal...
 
Top