I'm using a pygame.joystick.Joystick
object and want to be able to print a message asking the user to reconnect a usb joystick once it's been unplugged.
right now I have (roughly):
js = pygame.joystick.Joystick(0)
#... some game code and stuff
pygame.joystick.quit()
pygame.joystick.init()
while pygame.joystick.get_count() == 0:
print 'please reconnect joystick'
pygame.joystick.quit()
pygame.joystick.init()
js = pygame.joystick.Joystick(0)
js.init()
but it doesn't reconnect properly, idk what exactly it's doing, but it's definitely wrong. Any direction on this would be helpful
I managed to get mine working with Noelkd's suggestion, but I had a similar issue described by Ryan Haining
I had this issue too. I think you are correct, calling
quit
too often doesn't give the pad enough time to re-initialise - at least on my computer. I found that if you limit the calls to every second, it works.It can cause the player input to temporarily disconnect though, so any calls on a
joystick
won't work.It's better to only run this code if you detect that there has been no input for a while (say 5 seconds or something). This way you won't
quit
while a user is actually using the deviceUsage
Had to fire up the old xbox pad but I made a function that checks for disconnections and seems to work ok:
So if you run this function in your main loop it'll just keep running itself until it gets a joystick connection back. It works for the little test code I found:
http://programarcadegames.com/python_examples/show_file.php?file=joystick_calls.py
Also found:
http://demolishun.net/?p=21
Where I stole the idea from, he didn't have any code examples which was lame
And lastly, because you should always check the docs:
http://www.pygame.org/docs/ref/joystick.html