Quick Help : taskMgr.step()

So far i have a large amount of Python code doing stuff. It has its own Loop, and i want to run panda within that loop. So i should use taskMgr.step(). I do not fully understand panda as of yet. So far i have this for an example;

from direct.showbase.ShowBase import ShowBase
from panda3d.core import TextNode
from direct.gui.OnscreenText import OnscreenText
from direct.interval.FunctionInterval import Wait,Func
from direct.task.Task import Task
import time
import sys

class MyApp(ShowBase):

def __init__(self):
    self.accept("escape", sys.exit) #Escape quits
    ShowBase.__init__(self) 
    taskMgr.add(self.DoMath, 'DoMath')
    self.text1 = OnscreenText(text="nont", pos = (0.1, 0.7), scale = 0.1, mayChange = True)
    
def DoMath(self, task):
    self.text1.setText(str(time.time()))
    return task.cont

App = MyApp()
App.run()

My primary issue is that i want panda to run inside of a python loop something like this;

GameOn = 1
While GameOn = 1:


Call taskMgr.step() to keep panda happy

I cant seem to get the text1 to change with time.time() but that is another issue >:)

Thanks.

A better approach is to put the things you want to do each frame in their own task, and let Panda own the main loop: so that you continue to call app.run() to enter the main loop, instead of attempting to call taskMgr.step() repeatedly. Usually, the only reason to call taskMgr.step() is if you need to integrate with a third-party library, such as a windowing package, that also insists on owning the main loop (since there can be only one main loop in an application). But assuming you have no such needs in this case, you’re better off not trying to subvert Panda’s own main loop.

As for the time.time() update problem, I don’t see anything obviously wrong, though you should note that in certain cases, the value returned by time.time() updates infrequently in Panda due to a problem with single-precision floating-point. Instead of time.time() to measure elapsed time, it’s better to use globalClock.getFrameTime().

David

The manual said that too.

Making panda work inside my python loop means i can avoid re-writing my python code while i am still learning panda. This way i know what is working and what isn’t working. When i know more I can place the python code in pandas loop.

There is allot of difference between python code and pandas code, panda has allot that i am not used to yet. Better to change a little before i try to change allot.

If there is going to be any CRAZY glitches doing it this way id like to know. Other wise i will make a model on the screen move or make text appear, nothing fancy.

Thanks.

I think you should be able to make it work, just as you say: replace the call to app.run() with a while loop that includes taskMgr.step(). But still, I don’t recommend it.

You are going to need to learn to write tasks to use Panda effectively. It makes sense to learn that first. Once you understand tasks, it will be easy to structure your program using them, and then you can go on to other more advanced Panda structures.

David

Thanks for the recommendation, i will use tasks when i am better with panda. For now i have tryed to replace the call to app.run() with a loop that includes the taskMgr.step(), but no dice.

This is the code that works:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import TextNode
from direct.gui.OnscreenText import OnscreenText
from direct.interval.FunctionInterval import Wait,Func
from direct.task.Task import Task
import time
import sys

class MyApp(ShowBase):

def __init__(self):
    self.accept("escape", sys.exit) #Escape quits
    ShowBase.__init__(self) 
    taskMgr.add(self.DoMath, 'DoMath')
    self.text1 = OnscreenText(text="nont", pos = (0.1, 0.7), scale = 0.1, mayChange = True)
    
def DoMath(self, task):
    self.text1.setText(str(globalClock.getFrameTime()))
    return task.cont

App = MyApp()
App.run()

#gameon = 1
#while gameon == 1:

taskMgr.step()

This is the code that does not:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import TextNode
from direct.gui.OnscreenText import OnscreenText
from direct.interval.FunctionInterval import Wait,Func
from direct.task.Task import Task
import time
import sys

class MyApp(ShowBase):

def __init__(self):
    self.accept("escape", sys.exit) #Escape quits
    ShowBase.__init__(self) 
    taskMgr.add(self.DoMath, 'DoMath')
    self.text1 = OnscreenText(text="nont", pos = (0.1, 0.7), scale = 0.1, mayChange = True)
    
def DoMath(self, task):
    self.text1.setText(str(globalClock.getFrameTime()))
    return task.cont

#App = MyApp()
#App.run()

gameon = 1
while gameon == 1:
taskMgr.step()

Error is:
NameError: name ‘taskMgr’ is not defined

Could not find out why from searching forums, or the manual.

Thanks.

Well, you still have to instantiate MyApp. It’s in the init() function that all of the globals are created, including taskMgr.

David