2
\$\begingroup\$

I am developing a platformer in Pygame with the eventual hope of releasing it on Steam.

I am currently trying to add input support for multiple controllers.

To do this, I am using the Pygame joystick module, which allows a connected controller to send input events to my event queue (the event types are JOYBUTTONDOWN, JOYBUTTONUP, and JOYAXISMOTION).

I want to have input support for at least keyboards, Xbox 360 controllers, Xbox One controllers, and PS4 controllers.

The problem, of course, is that these controllers all use different button IDs.

One way I thought of getting around this was to call a function in my event processing code which could convert input from any of these devices to a more abstract event tag. For example, keyboard presses of "a", Xbox 360 controller presses of left on the D-pad, and negative motion on the seventh axis of a PS4 controller would all set an abstract "mapped_event" attribute to "left". I could then use this "mapped_event" attribute in my player sprite's process_event() method, which moves him based on event input.

Here's a sketch of how that conversion code would look:

def map_event(event):


    # KEYBOARD BUTTON PRESSES

    if event.type == pg.KEYDOWN:
        if event.key = K_RIGHT:
            mapped_event = "right"
        elif event.key == K_LEFT:
            mapped_event = "left"


    # CONTROLLER BUTTON PRESSES

    elif event.type == pg.JOYBUTTONDOWN:

        if joystick == "Xbox360 controller":
            if event.button == 3:
                mapped_event = "right"
            elif event.button == 2: 
                mapped_event = "left"       


    # CONTROLLER AXIS MOTION

    elif event.type == pg.JOYAXISMOTION:

        if joystick == "Xbox360 controller":
            if event.axis == 1:
                if event.value == 1:
                    mapped_event = "left"
                elif event.value == -1:
                    mapped_event = "right"

        elif joystick == "PS4 controller":
            if event.axis == 3:
                if event.value == 1:
                    mapped_event = "left"
                elif event.value == -1:
                    mapped_event = "right"

    return mapped_event

From what I can tell, this is the way a lot of people recommend getting around the inconsistent controller issue. For example, I think this is what is meant when people speak about an "intermediate layer" of input processing and "mapping from real buttons to game concepts". Please correct me if I'm interpretting this wrong.

My question is:

How can I extend this method, (or an alternative), to also support all controllers which use, say, XInput/DirectX (according to this answer, a lot of aftermarket controllers use these), while handling multiple OSes?

I am open to using a different framework for processing joystick events, provided the language is Python.


Related Questions

How can one impliment multiple control schemes for a single player?

Correct way to abstract an XBox Controller

Unity3D Joystick Buttons Xbox360 & PS3 have Inconsistent Button Inputs?

\$\endgroup\$
0
\$\begingroup\$

According to this talk by Lars Doucet and Jeff Bellinghausen, hard-coding input code for specific controllers is a naive approach to input support.

Even if you abstract to an intermediate layer, the approach is problematic for the followImg reasons:

  1. It makes key rebindings difficult
  2. It results in messy, ad hoc code
  3. It's not reliable given that different OSes and drivers may result in different button and axes mappings
  4. It's hard to display the correct button glyphs in-game

If you plan on releasing on Steam, a better way to achieve flexible controller support is to use the Steamworks Controller API, which is part of the Steamworks SDK.

From what I can tell, this would involve you purging your code of references to controller-specific input events in favor of more generic (higher-level) input actions (for example, "INTERACT", or "JUMP").

The Steamworks Controller API would take care of mapping specific controller inputs to these actions (you can specify a default mapping).

In addition to supporting many controllers, the Steamworks Controller API also allows users to customize their input schemes, which is always a nice feature.

The specifics of how to incorporate the Steamworks Controller API into your code can be found in the documentation.

If your game is written in Python, you may have to find a Python wrapper for the Steamworks API. There are a few out there, but they haven't integrated Steam's controller stuff yet, as far as I can tell.

The exact details of how to use the Steamworks Controller API in your Python code may deserve its own question.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.