Trying to add a read method to a timer that counts down


Im trying to make a countdown timer for a racing game. Thanks to panda.egg for inspiring me :slight_smile:

Right now the timer is working fine and I am trying to write a readRemainingTime() (see the code), but I’m having trouble accessing time of the task from this function. How do I do that?

Right now I get the following error:

Traceback (most recent call last):
  File "", line 38, in ?
    t = readRemainingTime( guiTimerTask )
  File "", line 35, in readRemainingTime
    return int( givenTime ) - int( task.time )
AttributeError: 'function' object has no attribute 'time'

I’m not very adept at coding python yet, but I have some java / general OO coding experience. Can somebody help me past this problem.

General suggestions are also welcome :slight_smile:


Code Follows:

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

guiTimer = DirectLabel()

givenTime = 30

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

def guiTimerTask( task ):
    remainingSeconds = int( givenTime ) - int( task.time )
    remainingMinutes = int( remainingSeconds / 60 )
    remainingHours = int( remainingMinutes / 60 )
    if remainingSeconds >= 0:
        guiTimer['text'] = 'Remaining Time ' + str( dCharstr( str( remainingHours ) ) ) + ':' + str( dCharstr( str( remainingMinutes % 60 ) ) ) + ':' + str( dCharstr( str (remainingSeconds % 60) ) )
        guiTimer['text'] = 'Remaining Time 00:00:00'
    return Task.cont

def increaseGivenTime( seconds ):
    givenTime = givenTime + seconds
def reduceGivenTime( seconds ):
    givenTime = givenTime - seconds
def readRemainingTime( task ):
    return int( givenTime ) - int( task.time )
taskMgr.add( guiTimerTask, 'guiTimerTask' )
t = readRemainingTime( guiTimerTask)
print t


I think its better to create a task that counts down an prints the remaining time and dont try to use more methods

And the second thing is that your countdown function has a parameter ‘task’ and your other function has a parameter ‘task’ but your countdownfunc doesn’t have a member called time. Maybe its better to add a global variable time where you store the task.time from your countdownfunc

Thanks for your reply Martin.

First I’ve become a bit more clear about my question since posting: What I am asking is if my other methods can access the time in the guiTimerTask. Or does only the guiTimertask’s method have access to it?

I need methods for getting/setting the current time from elsewhere. I have a timer and something else (which im not writing) will be asking my code if the timer is below zero. Can I achieve this somehow? I’ll try making a global time variable to store the timer, as you suggest.

Hiya Kapibara,
Welcome to the Panda 3D Forum :slight_smile:

Your timer seems to work fine if you remove - int( task.time ) from the line
return int( givenTime ) - int( task.time )

Have fun