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