timer propety out of rhythm

Hi, I am making a game in which the player moves according to music, every ipo and actions are controlled by a timer propety, which is multiplied by the framerate in which the animation was made.

So teorically this game could perfectly run at 20 f/s without having any lag (compared to the music, obiusly some shuttering would happen). So, to test if my game would run syncronized to the music I enabled all frames and run it.

The animation didn´t mached the music, it went faster. Then I debuged the timer propety and compared to a real timer and the propety went faster when the framerate was above 60 fps and slower when it was below that framerate.

So, the thing is… There is any way to force the timer to be an “absolute” (independent of framerate) timer?

I’ve attached a .blend showing the thing, just make sure you have V-sync turned off in your graphic card driver.

Thank you.

Attachments

crazy timer.blend (225 KB)

After some scripting, I have this:

 
### Absolute timer made by CGgap ###
 
# Get controller and owner
cont = GameLogic.getCurrentController()
own = cont.getOwner()
 
# Get variables
fps = GameLogic.getAverageFrameRate()
accrate = own['timer']
 
# Add current time to elased time
own['timer'] += 1/fps

I’ts much better than the timer property, but still have lag/overhead issues.

Please someone help me here.

you could use

from time import clock

cont = GameLogic.getCurrentController()
own = cont.owner

if not hasattr(GameLogic, "lastFrame"):
    GameLogic.lastFrame = clock()

own['timer'] += clock()-GameLogic.lastFrame

This adds the time in seconds since the script was last run to the owner’s “timer” property.

Works great!! thank you :D; just change += to = otherwise the timer goes nuts:

from time import clock
 
cont = GameLogic.getCurrentController()
own = cont.owner
 
if not hasattr(GameLogic, "lastFrame"):
    GameLogic.lastFrame = clock()
 
own['timer'] = clock()-GameLogic.lastFrame