0

I'm trying to make a program that uses a controller, and am programing support for multiple controllers. Everything works fine if I launch the program with a controller connected, and can even disconnect the device and connect another, but I get the error NameError: name 'j' is not defined when I start the program with nothing connected. I understand where the error is coming from, but am stuck on how to fix it.

#Looking for a Controller
while True:
    try:  
        pygame.joystick.init()
        j = pygame.joystick.Joystick(0)
        j.init()
        updateScreenConnected()
        
    except:
        updateScreenNoController()
        

#Main Loop
    events = pygame.event.get()
    pygame.event.set_grab(False)
    for event in events:
        if j.get_name() == ("Nintendo Switch Pro Controller"):
            randomFunction()
        if j.get_name() == ("Xbox One S Controller"):
            otherRandomFunction()

The error comes from the line referencing the Switch Pro Controller.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Talisman
  • 1
  • 1

0 Answers0