Working with sounds/animation in classes !containing run()

With a bit of a java background I’ve been learning panda and python for the last few weeks and have been able to set up some basic world, chasecam and basic physics and ‘even’ a loading screen, based initially on the roaming ralph example supplied with the engine.

I have however had a problem that I was unable to solve until today and now I’m quite curious as to what causes it and if its supposed to be this way.

In every panda example I’ve seen, there is just one class, which might or might not contain a class and has a run() function at the bottom.

In my game, I’m starting out with a main-like class, which loads an instance of ‘LoadingScreen’ and draws a button:

button = DirectButton(image = settings.TEXTUREPATH + "button.jpg", scale = 0.1, pos = (.0,.8, -0.7),
                      command=loadWorld)

The init function that is called, will instantiate a number of other classes, which take care of loading the world, background music, a player character, a keyMapper, menu’s etc.

Everything works fine with this. That is, models are loaded ok, the keyMapper works, I can move around my character, the camera works as intended… There is just one problem: I could not hear any sound and non-player characters did not show any animation, despite the fact that [sound].status did return ‘2’ which, according to the manual, means that the sound is playing. Sound itself is ok, I can hear the default sound when pressing a button.

After a few days of tinkering, I found that if I called the .loop(“animation”) on an actor or the .play() function on a sound from a Task, it would work. The task itself needs to be like this to work:

def runSound(self, task):
        if not self.sounding:
            self.mySound1.play()
            self.sounding = True
        return Task.cont

Ofcourse, creating a task for every sound I want to play and doing the same for animations, would be a bit… redundant (though I can of course restart the same task for different sounds). Over all, it just feels wrong…
can anyone enlighten me as to whether this is the intended way to do this? I have as of yet not been able to find any information regarding this ‘problem’ on the forum or the manual. Could I be doing something wrong at an earlier stage?

Thanks,
Roel

I don’t quite get what’s going on from your explanation, but I’m guessing that maybe the task manager isn’t running, or isn’t running properly.

In any application there needs to be exactly one call to run(), and it should be the last thing you do after starting up. This starts the task manager. Think of this as the main loop of the application: your startup procedure is to set up your loading screen, start any initial tasks or intervals, hang any initial messenger hooks, and then go get lost in run(). Thereafter everything runs in a task, or an interval, or is a response to a message.

That doesn’t mean you need a task for every sound or every animation. You can start a sound playing within another task that does other stuff too. But, you do need to write your code so that everything is in a task, and also that no task ever just goes to sleep waiting for something to happen–each task must always return promptly, or none of the other tasks will get a chance to run.

David

Alright, that makes sense. I somehow expected there to be some kind of general task, to which the animation would be added (which must happen in the startup phase). The code I tried to run would not work because there was indeed no task manager attached to it. (Being in the constructor, it was both outside the startup phase and not in a task. If I satisfy one of these constraints, it works). Thank you for clearing that up :slight_smile:

If I may make a suggestion, if this text would have been in this (Actor Animations) and/or this (Loading and Playing Sounds and Music) section of the manual, it would have saved me quite a few hours. Maybe someone could benefit from adding below or similar text from your post to the page(?)

The manual is a wiki. Please feel free to contribute clarifications and instructions to it, while do please try to keep it feeling more like a manual than like a wiki hodgepodge.

Click on the little dot below any of the manual pages, and give yourself a login.

David

I’ve added a little section about the task manager using roughly the text you provided in panda3d.org/manual/index.php/Actor_Animations

Roel

(I’ll be offline for the next week)