Pygame Joystick, Tasks, Events...almost there!

I was wondering if anybody could take a look at this bit of code.
I’ve taken the great examples in these forums and tried to put them together, but I still am having a minor issue. In the following, stripped down, version, when the JoystickPollingTask is added to the taskmgr, it loops forever via the “while 1:” and gets all of the buttons…everything works great here: the button presses are picked up and the event printHello accurately prints “hello” when button0 is pressed. However, the loadModels() function is never called since it is stuck in a while loop. How can we build this without the while loop? If I remove the while, then it never picks up the button presses. Any hints or fixes would be great!

from direct.task import Task
from direct.showbase.DirectObject import DirectObject
import direct.directbase.DirectStart

#Import pygame and init
from pygame import *
init()

class World(DirectObject):
    # constructor
    def __init__(self):
        init()
        j=joystick.Joystick(0)        
        j.init()

        self.loadModels()
        
        #add the joystick task to the taskmanager
        taskMgr.add(self.JoystickPollingTask, "hi")
    
        #print hello, if button0 is pressed
        self.accept("BUTTON0", self.printHello)
    #def
        
                
    def printHello(self):
        print 'Hello'
        
    def JoystickPollingTask(self, task):    
        print "JOYSTICK TASK!"
        while 1:
            for e in event.get():
                if e.type == JOYBUTTONDOWN:
                    if (e.dict["button"] == 0):
                        eventName = "BUTTON"+str(e.dict["joy"])
                        messenger.send(eventName, [])
                        print eventName                    
        return Task.done
    #def

    def loadModels(self):
        #Load the first environment model
        environ = loader.loadModel("models/environment")
        environ.reparentTo(render)
        environ.setScale(0.25,0.25,0.25)
        environ.setPos(-8,42,0)
    #def
        
world = World()
run()

I’ve used this as a reference: discourse.panda3d.org/viewtopic.php … e9e4b07c3b

For incentive, I am trying to write an Xbox360 controller handler class that I plan on releasing back to the Panda community. :slight_smile:

NOW is anyone interested? I haven’t had a chance to look at this problem yet this week, so don’t think I’m being lazy asking. I’ll hopefully have a chance to look into it this weekend. Any tips would be great!

I think you can just remove “while 1:”. event.get() will get all events since the last time you called event.get() and the taskMgr will call your JoystickPollingTask every frame, so there is no need for the while loop there.

I hope this helps.

in addition to Laurens’ post: also change the task.done to task.cont, otherwise it won’t work.

Thanks for replying.
Yeah, I tried removing “while 1:” because, in theory, you are correct. It should be called every frame since it is a task. Unfortunately, by removing “while 1:”, the joystick appears unresponsive because “print JOYSTICK TASK!” and printHello() never print anything.

If you add “while 1:” the joystick works, and the printing messages work great, but of course, the game is now locked in the that loop and can’t do anything else.

HA! Thanks!
Task.cont fixed it! I knew I missed something.

The fix is to remove the “while 1:” and add Task.cont.

I will post here when I manage to get this thing to work fully.
Thanks! :slight_smile:

OK, as promised here it is!

pyPad360 v1.0 is released!
https://discourse.panda3d.org/viewtopic.php?p=13314#13314