Question about the main loop and run()

Ok im about to create a loop finally for my game. Can somone clarify what run() does? Where should i put it in regards to my main loop. WOuld it work if i put it before? or do i constantly need to call run() inside the loop. In panda i only need to render things once and they will stay up there until i destroy them?

If you want your game to have a main loop:

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

def mainLoop(task, args):
    # Do whatever stuff here
    return Task.cont

taskMgr.add(mainLoop, 'My Main Loop')

You can only call run() once, then any further command is ignored (correct me if I’m wrong).
Use the taskMgr.add() function to add a new thread to loop through. Alternatively, you could use threading, but as I’m am not an expert on that subject, I’ll leave it to someone else to explain that.

The argument task for the mainLoop function is required for taskMgr.

And yes, models exist until the application quits or you destroy them.

It is better to use tasks for most things like has been mentioned, but if you want to really control the loop yourself you can use:

while(True):    #or at least until your done...
    #before panda stuff code
    #after panda stuff code

Do not call run if you do it this way because run basically does this anyways. You can still add tasks like you would normally and can do a hybrid, but it is probably better to stick with one way or the other.

xboxjoshone’s example will execute whatver you put into the task once every frame that is rendered.

Mindstormss’s example will function, mostly, but run() does do more than just call taskMgr.step()

The ‘spirit’ of panda is that all your logic is handled in tasks, and the run() command causes the engine to render the frames, execute your code, etc. Everything is event driven, either based on a frame render, keystrokes, mouse moves, etc.

Tasks can be set to run at time intervals as well, if needed. The manual does a good job of explaining this.

I would recommend that you think of your game in these terms, as it does make the whole thing cleaner, by working inside the parameters that Panda was designed for.

There are at least two threads in the past month asking this same sort of question. You might poke around and see if you learn anything more there. But the gist of the answer is what everyone already said: let Panda handle the “main loop” and put your own code in tasks/callbacks as needed.