manual -> taskMgr confusion

Hi! After reading through the taskMgr manual section, I built some code:

import direct.directbase.DirectStart
from direct.gui.DirectGui import *
from direct.task import Task
import time,sys

class World(DirectObject):
        def __init__(self):
                self.x=0
                self.list=[]
                task=Task.Task(self.blubb)
                #taskMgr.add(task, 'myTask')
                taskMgr.doMethodLater(1, task, 'myTask', extraArgs = [task])

         def blubb(self,task):
                if self.x>= 12:
                        sys.exit()
                        return(Task.done)
                else:
                        self.list.append(task.time)
                        print self.list
                        self.x=self.x+1
                        return Task.cont

        def monitor(self,task):
                print taskMgr
                return Task.cont

w=World()

run()

When I am running the code as stated above, everything is fine, but when I change the comment sign like this:

#taskMgr.add(task, 'myTask')
taskMgr.doMethodLater(1, task, 'myTask', extraArgs = [task])

I get the following error message:

  • But following the manual, the “doMethodLater” is nothing else than the “add”, except that its initiated when the time given is over?

Where is my problem of understanding it?

Regards, Bigfoot29

Try replacing the doMethodLater() call with:


taskMgr.doMethodLater(1, self.blubb, 'myTask')

It looks like doMethodLater() expects to receive a function, not a Task object. It will always create a new Task object for its purpose.

This is a little bit different from the way taskMgr.add() works, for no good reason. I will fix it so that it can receive a Task object instead, so that the two methods will be more similar.

David

Thanks for the (real) fast reply :slight_smile:

The question is: how can I get the time of the task when adding it with doMethodLater?

Regards, Bigfoot29

The above modified call should do it. The doMethodLater() call will create its own Task object, rather than accepting the one you give it; and it will pass this Task object to the method as the one parameter. You can then query task.time on that object.

David

I am sorry, you are right.

import direct.directbase.DirectStart
from direct.gui.DirectGui import *
from direct.task import Task
import time,sys

class World(DirectObject):
        def __init__(self):
                self.x=0
                self.list=[]
                taskMgr.doMethodLater(1, self.blubb, 'myTask')

        def blubb(self,task):
                if self.x>= 12:
                        sys.exit()
                        return(Task.done)
                else:
                        self.list.append(task.time)
                        print self.list
                        self.x=self.x+1
                        return Task.cont

        def monitor(self,task):
                print taskMgr
                return Task.cont

w=World()

run()

… is doing the job as planned :slight_smile:

Thanks, David!

Regards, Bigfoot29

If my task contains arguments, in what way do I need to write?
taskMgr.doMethodLater(1, self.blubb, ‘myTask’, extraArgs = [XXX, argu])
XXX = ???

Thanks!

If blubb is defined thus:

def blubb(self, a, b, c):
   ...

Then you should pass extraArgs = [a, b, c]. Note that your function is not expected to have an implicit task parameter. If you want your function to receive a task parameter, in addition to your own extra arguments, it’s difficult to do with the current definition of doMethodLater().

David