Search code examples
pythonpython-3.xsdl-2

How do I utilize the pygame._sdl2.controller module? Does it even exist?


I'm in the throes of producing a game, and with my keyboard controls going well, I decided to start expanding into controller support. Following the documentation, I thought I had initialized pygame.controller and created two controller objects, but the compiler rejects it, saying that the controller module doesn't exist. And checking the auto-completion for pygame., that is absolutely the case; I see the pygame.joystick module, I see a whole bunch of controller events (i.e. CONTROLLER_BUTTON_A and CONTROLLERAXISMOTION, all things listed in pygame._sdl2.controller's documentation), but I see no controller object itself.

I originally started with pygame.controller, but on closer examination, I changed it to pygame._sdl2.controller, as per the documentation. No success; compiler still throws an error, saying _sdl2 is not an attribute of pygame. I've searched to see if any other developers have been having similar problems, but it almost feels like I'm the first person to ever try to use the module - everyone else seems to use pygame.joystick, with no mention of pygame._sdl2.controller. And I would absolutely do the same; it just seems really convenient to scan for all controller button presses (and releases!) and comparing them to events like CONTROLLER_BUTTON_A.

So am I just missing something here? Do I need to update SDL2 or something? (Is that even a thing?) Is there a different way to fetch all button presses so I can compare them to the CONTROLLER_BUTTON events? (EDIT 2: I know that pygame.joystick has similar methods, they just aren't ABXY specific like pygame._sdl2.controller methods are documented to be.) I'm using what I believe to be the latest version of pygame (2.5.0). Thanks in advance for any help.

EDIT: Okay, on closer inspection of the pygame.joystick module, it seems very powerful, and it has mappings for modern controllers, which is useful. Though I'm probably gonna start moving in that direction, I'm still curious about this pygame._sdl2.controller thing; is it just deprecated, or what?

EDIT 2: Cool, my question got downvoted and closed as a duplicate for some reason, even though the "duplicate" provided does not answer my question in any way. The provided answer has nothing to do with the pygame._sdl2.controller module, only details pygame.joystick, and does not satisfy the questions of "how do I use the pygame._sdl2.controller module," and "does the Controller module even exist". I very much understand that pygame.joystick seems like a better solution, but it still doesn't answer why there's a pygame._sdl2.controller module that doesn't seem to work at all. Considering it's in the documentation - "controller", under "other", at the top - it'd be nice to know why it's in the docs, even though it's apparently not even available as a class of pygame.


Solution

  • This question had been answered in the comments 16 days ago. Mattiis's response was:

    Yeah, the tutorial side of _sdl2.controller is pretty much non-existent to my knowledge/ If you want to use anything from _sdl2, you need to explicitly import it: import pygame._sdl2, I usually like to do it like so import pygame._sdl2 as pg_sdl2 and then I'd use it as pg_sdl2.controller... Not sure either why this was closed because your question is obviously about _sdl2.controller, though I have to say again that there's not that many that could tell you much about it. Likely best to join the pygame discord server if you want to discuss this module.

    This was the exact answer to my question; I was unaware that there was a pygame._sdl2 module that needed to be imported before I could access anything from SDL2. However, the key takeaway here is that pygame.joystick module is very powerful, it is more closely supported by the community than pygame._sdl2.controller, and the joystick documentation has button mappings for various popular controller types.