A question about run() main loop

After run(), any codes won’t effect. When I want to change some attributes of my “Pet” object after run, how to?

My pet will do different actions according to its attributes, and its attributes will be changed in runtime(other program will call Pet’s function to change its attribute), so how to deal with such a situation?

Modify the stuff in tasks or events (add functions to the task manager or accept events, in those functions you can put whatever code you want).

Thank you pro-rsoft.

Could you show me some pieces of codes?
The manual and the examples don’t show any taskMgr’s usage.
And I dont’s know yet how to use taskfunction.

Then you didn’t look good enough. Check the manual, section J, “Tasks and Event Handling”. Or, read up in the sample programs.

Thanks.

What I want to say is that I have really read that. Hmm.

Maybe there is a misunderstand in my descriptions.

Changing my object’s attribute is not by mouse or keyboards, just function calling.

I image that there is a pet object ,and run(). Then other program will import Pet, and call function such as Pet.setAge().

Then my pet will be bigger. So what I want to say is its bigger is not by mouse’s press or hit the keyboard, just other codes’ calling.

So I have not found how to do. Bcz I think what task will do depends on what happend by mouse or keyboards.

When do you want to have those ‘other codes’ executed then? Right after run()? If so, there shouldn’t be much difference if you put run() after those codes?

If you want it to be executed, say, 5 seconds after the window opens, use taskMgr.doMethodLater (before you call run().)

For example, there are 2 modules in one software(a pet software). One is the UI and another is my panda engineer module.
At the beginning, the pet’s age is 1. When user feed the pet day after day(operate by UI), after 5 days, the pet’s age is 2.
I think UI should call Pet’s function setAge(2),the parameter is 2. Then I should scale the pet.

So what should the UI call Pet.setAge function? Before run() or after? I think that is after.

well… you’r describtion is a bit fuzzy and it sorta sounds like you have a wrong idea about how things are working.

to keep things as simple as possible.
-run() will start your game.
-if you want your pet to grow older you need to set up a task which increases the age of your pet every x seconds.
-if you want your pet to respond to mouseclics or keypresses, you need to accept keyboard/mouse events and use them to call the neccessary function.
-if you create a UI with a button, you have to make the button to call the function you want.

this usualy is all done before calling run()

you should study the examples, starting with the simple ones (solar system). most accept some sort of input, such as key presses. some have UI such as the musicbox, some feature tasks which execute functions every frame or every x-seconds.
those are all basic usage of panda and the examples cover all those issues, providing well commented code.

Basic misconception I’ve seen frequently with students transitioning from C/C++/Java/Basic to Panda/Python: Panda/Python is not C/C++/Java/Basic. The notion of a main loop in which you execute your code does not exist in Panda if you’re using Panda correctly.

For taking input, rather than setting up a while(1) loop and polling your inputs, you set up an event handler to automatically trigger a callback routine when an input changes. The same system can be used to post your own events from one piece of code and react to them in another.

For timing, rather than setting up a while(1) loop and checking your dt every iteration (or worse, using a wait() or sleep() call to regulate time), set up a background task (taskMgr.add(myTaskMethod,“myTaskName”,…) - it IS in the manual) and do what you want in the associated method. As long as the method takes a task argument and returns Task.cont, it will be executed repeatedly as frequently as the Panda has time to schedule it. task.time() (that’s your method’s input/argument ‘task’, not the ‘Task’ class) will tell you how much time has elapsed since the task was added to taskMgr, and there are other systems that exist for tracking how much time has passed since your game started.

For moving stuff continuously, you can set up a background task and manage the state yourself, but it’s generally preferred to use Panda’s powerful Interval system to chain and composite any transformations that can go on independently of user input.

Any such tasks/intervals/etc. can be set in motion in the init routine of your main game class, and can change the state of anything in your game (provided you have a sufficiently persistent handle to the state) whenever they execute, whether it’s when you first instantiate your game class or hours after run() sets everything in motion.

Again, there is no main loop in Panda from a user perspective, and any attempt to write your own main loop will cause the associated method to spin, blocking all other execution (including anything you may be waiting for in the loop, and ALL graphics and other state updates) until the loop ends.

@loneKiltetNinja : you might want to say: you should not write your own mainloop. cause panda is really flexible. you can of course write your own main loop if you want to, and even calling rendering from within your own mainloop is quite easy. but “usualy” one would not do this unless you have a good reason for it. still, it IS possible.
aswell as it’s possible to put your mainloop 1:1 into a task and do polling there, also works. about blocking. the only time panda will be blocked is when you block it with a function which might take ages to return something (such as wait(10) ). it doesnt matter if you write a mainloop and put such a function in, or if you run a task with such a function. if you block panda it’s your fault :wink:

Which is what I was getting at.

[initstuff]
while(1){
     [gamestuff]
}
return 0;

is standard in many languages, but

[initstuff]
while True:
     [gamestuff]
return 0

will usually K.O. the Panda. And that’s a surprisingly intuitive thing to try putting in your init routine or right after run() if all you’ve seen are c++ projects.

How does one go about overriding the main loop in Panda anyway, short of tossing a background task from init and pretending it’s the real thing?

I just think of it as -
with run(), Panda’s already written the main loop for me :slight_smile:

(I just gotta add the dtls)

@LoneKiltedNinja: like i said you CAN write your own loop. writing your own loop in a while(1) or while true (which is basically the same in python would simply require you to call rendering, taskmanaging etc INSIDE your loop. it’s not the loop killing panda, it’s you cause you didnt call the neccessary functions to render or handle tasks etc yourself. so to say writing a main loop will work. but writing just some loop will not.

silveralex is right. run() already calls a well-made mainloop and you should use it. and rely on events and the taskmanager :slight_smile:

Ah, I gotcha. Didn’t know said render/manager/etc. update calls were readily accessible for manual use.

Still, as my department manager would say, I think we’re in violent agreement over the fact that there are less-involved, or at least more-intended ways of doing the originally proposed tracking of time/state/etc. than writing your own main loop. :confused: