Alternative timing for tasks

Hi all -
I’m using Panda3D in an experimental setting, and am using timers to do things like record the user’s position / orientation at 5 Hz (every .2 sec). I’m currently using a task that calls itself with the ‘taskMgr.doMethodLater’ method after a delay of .2 seconds and then returns Task.done, but is there a less clunky way to run this kind of a timer?
Can you set up the timer to run every X seconds instead of every frame? For example, in the Vizard VR toolkit (a commercial package similar to Panda3D) you can set up a timer with ‘viz.starttimer( , , )’ and explicitly control the timing so that it’s only running when it needs to be.
Just wondering if a similarly elegant solution was available in Panda. If not, it might be worth integrating into future versions with task interval as an optional argument to the ‘taskMgr.add( )’ method.

Yep, your klunky approach is currently the best approach to do this in Panda. You’re right, that sounds like a fine addition.

David

Have you tried having the task run every frame and only record the data when task.Time is a multiple of .2? It would mean plenty of extra wasted computing, but might be easier than the doMethodLater workaround.

I am pretty sure there a ‘Task.again’ you can return (instead of ‘Task.done’), so you dont have to call ‘taskMgr.doMethodLater’ each step.

I suppose you could emulate this kind of timer: ‘viz.starttimer( , , )’ easly.

You could create your own task method that would automatically check a local time counter and see if he needs to run this frame or not.

class VizTimer:
  def __init__(self,begin_tm,end_tm):
    self.now_tm = 0
    self.before_tm = 0
    self.begin_tm = begin_tm
    self.step_tm = step_tm
    self.end_tm = end_tm
  def should_run(self,now_tm):
    self.before_tm = self.now_tm
    self.now_tm = now_tm
    return self.begin_tm <= self.now_tm and self.end_tm >= self.now_tm
  def handler(self,task):
    if self.should_run(now_tm):
      -- your code here --