import time
import panda3d.core
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
base = ShowBase()
timeFromLastFrame = 0
def doMethodLater(task):
global timeFromLastFrame
currentTime = time.time()
elapsedTime = currentTime - timeFromLastFrame
print(elapsedTime)
timeFromLastFrame = currentTime
return task.again
base.taskMgr.doMethodLater(1/60, doMethodLater, "doMethodLater")
base.run()
I’d expect the task to run every 1/60 seconds, but the output log is very strange:
0.033344268798828125
0.03333926200866699
0.016874313354492188
0.03317904472351074
0.016629934310913086
0.0333251953125
0.03328251838684082
0.0167236328125
0.016646146774291992
0.033270835876464844
0.033332109451293945
0.01676487922668457
0.033345699310302734
0.032674312591552734
0.016810178756713867
0.017063379287719727
0.016716480255126953
0.033325910568237305
0.01668071746826172
0.03327798843383789
0.016716480255126953
0.03332853317260742
0.03332114219665527
0.016675233840942383
0.033351898193359375
0.01786661148071289
0.032109737396240234
0.016745328903198242
0.03325295448303223
0.016701698303222656
0.03330278396606445
0.016758441925048828
0.03318929672241211
0.0167386531829834
0.03331899642944336
0.03334999084472656
0.033368825912475586
0.0332491397857666
0.0172576904296875
0.032778263092041016
0.016712665557861328
0.03325390815734863
0.016722917556762695
0.03332257270812988
0.016674041748046875
0.01669907569885254
0.03332233428955078
0.016642093658447266
0.03333163261413574
0.03332662582397461
0.016921281814575195
0.03332710266113281
0.03311443328857422
0.03335428237915039
0.03328752517700195
0.016675949096679688
0.01673102378845215
0.03328204154968262
0.0333712100982666
0.033332109451293945
0.03329968452453613
0.01665639877319336
0.016652822494506836
0.03327345848083496
0.01674675941467285
0.016788721084594727
0.03318953514099121
0.016715049743652344
0.03320670127868652
0.0167083740234375
0.0167849063873291
0.033277273178100586
0.03327798843383789
0.016729116439819336
0.016688108444213867
0.03325533866882324
0.03338193893432617
0.016707420349121094
0.033222198486328125
0.016717910766601562
0.016697406768798828
0.033333778381347656
0.01666116714477539
0.03333592414855957
0.032968759536743164
0.0169827938079834
0.016723155975341797
0.03329801559448242
0.01673746109008789
0.03332328796386719
0.03358817100524902
0.032286882400512695
0.03357100486755371
0.01674818992614746
0.017148256301879883
0.03331279754638672
0.017984628677368164
0.0322420597076416
0.03321099281311035
0.016751766204833984
0.0333859920501709
0.0332949161529541
0.03373861312866211
0.03329205513000488
0.016692399978637695
0.03321385383605957
0.016788959503173828
0.03335404396057129
0.01812434196472168
0.031736135482788086
What is going on?
How can I be sure to run a function every 1/60 seconds, without using ordinary tasks? I don’t want certain functionality tied to the refresh rate, while it still being called in specific time intervals. I know getDeltaTime() can be used in some cases such as transforms to avoid certain functionality to be dependent of framerate dips or increase, but it’s not a perfect solution when you just need to perform other functionality in specific time intervals. I hoped doMethodLater tasks would aid here, but it seems like they are simply still ran every frame, merely a delay is applied based on the time you specified, rather than the delay being the time interval between which the functions will be called. Even if we ignore the 0.033 second values in the log, the 0.016 ones aren’t really accurate themselves.
Using Sequences with a function interval and Wait inside doesn’t do the trick either:
import time
import panda3d.core
from direct.showbase.ShowBase import ShowBase
from direct.interval.IntervalGlobal import *
base = ShowBase()
timeFromLastFrame = 0
def methodToRun():
global timeFromLastFrame
currentTime = time.time()
elapsedTime = currentTime - timeFromLastFrame
print(elapsedTime)
timeFromLastFrame = currentTime
funcInterval = Func(methodToRun)
mySequence = Sequence(funcInterval, Wait(1/60))
mySequence.loop()
base.run()
0.036360979080200195
0.016874313354492188
0.016983747482299805
0.016647815704345703
0.01668071746826172
0.01659679412841797
0.016788721084594727
0.016569137573242188
0.016723155975341797
0.01656198501586914
0.033400535583496094
0.016652822494506836
0.016262054443359375
0.01653289794921875
0.01680755615234375
0.017047882080078125
0.0179440975189209
0.01542806625366211
0.016661643981933594
0.016650676727294922
0.016713380813598633
0.016632556915283203
0.01662158966064453
0.016775131225585938
0.016597509384155273
0.01668548583984375
0.016673564910888672
0.01665782928466797
0.01663827896118164
0.016702651977539062
0.016625642776489258
0.01668381690979004
0.01663804054260254
0.016623497009277344
0.01675701141357422
0.016595840454101562
0.01672840118408203
0.016658782958984375
0.01658153533935547
0.016684770584106445
0.016745805740356445
0.016600608825683594
0.016776084899902344
0.01656365394592285
0.0166776180267334
0.01668095588684082
0.016765832901000977
0.01653575897216797
0.016766786575317383
0.016631364822387695
0.016571998596191406
0.016719818115234375
0.01663661003112793
0.016740083694458008
0.01671886444091797
0.016659975051879883
0.01664900779724121
0.01661372184753418
0.017797470092773438
0.015555620193481445
0.03335404396057129