How to use Timer, a small example maybe?

How to use Timer, a small example maybe? I would like it to strat from 00:00:00 at the beginning of the game and count the time all the way down to the end.
Thanks

Well, here’s a little example.


import direct.directbase.DirectStart
from direct.gui.DirectGui import *
from direct.task import Task

mytimer = DirectLabel()
mytimer.reparentTo(render)
mytimer.setY(4)

def dCharstr(theString):
  if len(theString) != 2:
    theString = '0' + theString
  return theString

def timerTask(task):
  secondsTime = int(task.time)
  minutesTime = int(secondsTime/60)
  hoursTime = int(minutesTime/60)
  mytimer['text'] = str(hoursTime) + ':' + dCharstr(str(minutesTime%60)) + ':' + dCharstr(str(secondsTime%60))
  return Task.cont

taskMgr.add(timerTask, 'timerTask')

run()

Hope this is what you want! :wink:

Thanks a lot !
It works.
But do I really have to reparentTo(render)? Because, when I do that, I can not see the label on the screen. And I see it only, when I skip that reparent part.
Also, is it possible to make a button transparant?

Thank you

Right, you don’t need to reparentTo(render) unless you want it to be in the 3-d world. If you want the label to appear on the screen glass (the 2-d scene), that’s the default.

To eliminate the default background (and make the label transparent), try:


mylabel['relief'] = None

David

Well, I leave it 2d if I want it to be part of the HUD(Heads Up Display) otherwise, I put it in the 3d scene graph and put it right on the surface of a score board or something(so the players have to go up and look at it or something)

That’s just my style, though. It’s your game, your decision!

I simplified the code a bit by using string replacements. Thanks for the example, very useful also if 7 years later :slight_smile:

import direct.directbase.DirectStart
from direct.gui.DirectGui import *
from direct.task import Task

mytimer = DirectLabel()
mytimer.reparentTo(render)
mytimer.setY(7)


def timerTask(task):
  secondsTime = int(task.time)
  minutesTime = int(secondsTime/60)
  hoursTime = int(minutesTime/60)
  mytimer['text'] = "%02d:%02d:%02d" % (hoursTime, minutesTime%60, secondsTime%60)
  return Task.cont

taskMgr.add(timerTask, 'timerTask')

run()