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:
DirectStart: Starting the game.
enumerate is already present in builtin
Warning: DirectNotify: category ‘Interval’ already exists
Known pipe types:
glxGraphicsPipe
(all display modules loaded.)
:express(warning): Adjusting global clock’s real time by -0.562996 seconds.
Traceback (most recent call last):
File “tasks.py”, line 30, in ?
run()
File “debtmp/usr/share/panda3d/direct/src/showbase/ShowBase.py”, line 1603, in run
File “debtmp/usr/share/panda3d/direct/src/task/Task.py”, line 781, in run
File “debtmp/usr/share/panda3d/direct/src/task/Task.py”, line 728, in step
File “debtmp/usr/share/panda3d/direct/src/task/Task.py”, line 671, in __stepThroughList
File “debtmp/usr/share/panda3d/direct/src/task/Task.py”, line 600, in __executeTask
File “tasks.py”, line 19, in blubb
self.list.append(task.time)
AttributeError: Task instance has no attribute ‘time’
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
drwr
January 24, 2006, 7:11pm
2
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
The question is: how can I get the time of the task when adding it with doMethodLater?
Regards, Bigfoot29
drwr
January 24, 2006, 7:20pm
4
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
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!
drwr
March 12, 2006, 9:23pm
7
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