Doubt about timers

I’ve created a class to handle time(notice, I’ve not compile this guy, just wirte the code in notepad):

class Timer(DirectObject.DirectObject):
   def __init__(self):
      self.TotalTime  = 0
      self.TimerLabel = DirectLabel()

   def GenTimerString(self, task):
      seconds = self.TotalTime % 60
      minutes = self.TotalTime / 60
      TimerLabel['text'] = str(str(minutes) +  ' : ' + str(seconds))
      self.TotalTime -= 1

   def StartTimer(self)
      taskMgr.add(self.GenTimerString, 'GenTimerString')

   def AddTime(self, timeToInsert):
      self.TotalTime += timeToInsert

   def DecreaseTime(self, timeToDecrease):
      self.TotalTime += timeToDecrease

   def InsertTotalTime(self, TotalTime):
      self.TotalTime = TotalTime

I’ll use in my game. My intention is, this timer runs when my player starts to fly. So I create a single method to add the
timer method in taskMgr. My doubt is: how can I do for timer method waits 2 seconds to decrement the total Time? I’ve think to use the

Wait() function. But I’ve seen in the forum that has a function in the taskMgr called doMethodLater that I can put the time in this function

and it waits to execute a function I determined. But my fear is when the seconds pass by the function will continues anymore. If the doMethodLater will continues to execute this infinitely, what’s the better approach to use? Wait or doMethodLater?
Sorry if I wasn’t clear. If you notice, my main questions are timer and performance.

Thanks for all for the support

doMethodLater adds your method to taskMgr with execution delay, and that’s it. Nothing more, nothing less.
Whether your method would continue or not, it depends on your method’s return value. If it returns Task.cont, then it would be executed again next frame.
Open your (in direct/src/task):

class Task:

    # This enum is a copy of the one at the top-level.
    exit = -1
    done = 0
    cont = 1
    again = 2
# Check the return value from the task
if ret == cont:
    # If this current task wants to continue,
    # come back to it next frame
    taskDoneStatus = cont
    frameFinished = 1
elif ret == done:
    # If this task is done, increment the index so that next frame
    # we will start executing the next task on the list
    self.index = self.index + 1
    taskDoneStatus = cont
    frameFinished = 0
elif ret == exit:
    # If this task wants to exit, the sequence exits
    taskDoneStatus = exit
    frameFinished = 1
def __repeatDoMethod(self, task):
    Called when a task execute function returns Task.again because
    it wants the task to execute again after the same or a modified
    delay (set 'delayTime' on the task object to change the delay)

Contradictive, isn’t it ?

thanks for the support ynjh_jo, I’ll test it soon.

this function just waits the time you set and after this continues your execution indefinitely(if return Task.cont)
What I want is: the function executes each 2 seconds, and not waits 2 seconds and executes indefinitely
any suggestion?

this should do it :

def myM(t):
    print 'XXXXXXXX',globalClock.getFrameTime()


Do NOT do that. That way you recreate the task over and over again! It’s better to return task.again from within the task function:

def myM(t):
    print 'XXXXXXXX',globalClock.getFrameTime()
    return t.again


By the way. t.time also will return the frame time.

Well, C#er clearly said that the task must repeat every 2 secs WITHOUT the first delay.
About spawning the task again & again, you’re right (BTW the tasks list didn’t increase, but yes doMethodLater still has to do needless lines), so this works too :

def myM(t):
    print 'XXXXXXXX',globalClock.getFrameTime()
    return t.again


I solved this with this solution. If anyone wants, here it is.
Thanks for all for the replies

import direct.directbase.DirectStart
import time
from direct.showbase import DirectObject
from direct.gui.DirectGui import *
from pandac.PandaModules import *
from direct.task import Task
from direct.interval.FunctionInterval import Wait

class Timer(DirectObject.DirectObject):
   def __init__(self):
      self.timeVal = 0
      self.Text = DirectLabel()
      self.Text['text']       = "Alex"
      self.Text['text_fg']    = (1,1,1,1)
      self.Text['text_scale'] = (0.07, 0.07)
      self.Text['text_pos']   = (1.27, -0.57, 0.0)
      self.Text['text_font']  = self.TimerFont = self.CreateTimerFont()
   def CreateTimerFont(self):
       return loader.loadFont('FABIAN.TTF')
   def SetTimerValue(self, timeVal):
      self.timeVal = timeVal
   def StartTimer(self):
      self.theTime = time.time()
      taskMgr.add(self.DecreaseTimer, "DecreaseTimer")
   def DecreaseTimer(self, task):
      timer = time.time()
      if(int(self.theTime + 0.6) < int(timer)):
         self.theTime = time.time()
         timeMin = str(int(self.timeVal/60))
         timeSec = str(int(self.timeVal % 60))
         if(len(str(timeMin)) < 2): timeMin = str('0' + timeMin)
         if(len(str(timeSec)) < 2): timeSec = str('0' + timeSec)
         self.Text['text'] = str(timeMin+':'+timeSec)
         self.timeVal -= 1
      if(self.timeVal <= -2):
         self.Text['text'] = '00:00'
         return Task.done
      return Task.cont