# need some explanation on this bit of code

• 10-07-2009
Masterx
need some explanation on this bit of code
hello all, i have a pretty simple bit of code, well i know what it does in all, but i dont get it precisely.
consider the following bit of code :
Code:

```void Game::timerUpdate(void) {     double currentTime ;     currentTime = timeGetTime() - lastTime;         if (currentTime < 33.333 )         return ;     frameCounter++;         lastTime = timeGetTime();     }```
that if statement! and the return statement afterward ! what is it doing?
i mean it is supposed to make a delay or sth? if so how is it doing it ?
• 10-07-2009
Daved
Yes, it does seem to be a delay. It is checking the current time against the last time the frame counter was updated. If it is 33.333 or more, then the frame counter is incremented. It does this by leaving the function immediately with the return statement if the time elapsed is less than 33.333. The return statement means the function ends and the code after it does not get called, including the increment of frameCounter.

Presumably, the function is called over and over in a loop of some sort, so even if the frame counter isn't updated this time, it will be called again and again until it does get updated.
• 10-07-2009
Masterx
thankyou Daved,
i made sth here , and the problem is i get into a loop! and that would always increment the framCounter?

its here :
Quote:

initialization stuff :
currentTime = 0
lastTime = 0

the function :

currentTime = now() - lastTime

if currentTime <33.33
exit

else
frameCounter ++
lastTime = now()
end(or exit)
---------------------------
sample input
figures are in milliseconds .

1. 1000 = 1000 - 0
1000 ! < 33

then frameCounter++
last = now = (1500) //because its in the same function, i assumed the time difference is not that much, so i added 500+)

--------------------------
(again in the next call, i assumed a bigger quantum is passed !compared to the one in the function , so i added 1000+ for each call )

2. 1 500 = 2000-500
1500 !<33.333

then frameCOunter++
last = 2000

---------------------------
the same thing applies here too ( 1000+ for the call and 500+ for updating the lastTime)

3. 1000=3000-2000

1000 !<33.33
then frameCOunter++
last = 2500

-----------------------------
4. 1500 = 4000-2500

1500 !<33.33
then frameCounter++
last = 3000
so we came back on what happened in stage 2 actually ! and if we go this way the same process repeats ! so how it is possible for a system to actually works this way! ?
i saw it working! it works! but i cant make myself get it!
i make myself a sample input and all i get is just nonsense!
---------------------------
• 10-07-2009
brewbuck
If you rename the currentTime variable to deltaTime (or timeChange) does it make more sense?

You might be confused because the calculation being performed doesn't seem to be computing the "current time," and you'd be correct to think that, and correct to be confused.

The variable name is awful and inexcusable.