1

How do I detect the game controllers layout from apple tv? I want to change the controls if the layout is different for the controller which will make the game easier to play. .For example, the apple recommended Nimbus Controller is shaped like a play station controller with two joysticks at the bottom, but I have seem other types of controllers which have a xbox like design with a d-pad and a joystick at the bottom instead, if I can detect which is which, to change to controls for different controllers, it will make the game easier to play

Any help would be appreciated

KingCoder11
  • 415
  • 4
  • 19

2 Answers2

2

You should use controller profiles to map physical controls to game inputs.

Controllers are automatically discovered, a physical controller is represented by a GCController object, which ‘profiles’ the controllers controls such as GCGamepad, extendedGamepad etc. You should check which controls are registered to each controller. From the documentation on Discovering And Connecting Controllers:

“After your app has finished launching, the operating system automatically creates a list of connected controllers. Call the controllers class method to retrieve an array of GCController objects for all connected controllers.”

In apples sample code they register for .GCControllerDidConnect Notifications and cast the notification object as a GCController instance to a function that set’s up the controls if they exist, parse the controller and assign the corresponding handler method:

NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.handleControllerDidConnectNotification(_:)), name: .GCControllerDidConnect, object: nil)

@objc func handleControllerDidConnectNotification(_ notification: NSNotification) {
    let gameController = notification.object as! GCController
    registerCharacterMovementEvents(gameController)
}


  private func registerCharacterMovementEvents(_ gameController: GCController) {
      //…

    // Gamepad D-pad
    if let gamepad = gameController.gamepad {
        gamepad.dpad.valueChangedHandler = movementHandler
    }

    // Extended gamepad left thumbstick
    if let extendedGamepad = gameController.extendedGamepad {
        extendedGamepad.leftThumbstick.valueChangedHandler = movementHandler
    }


      //…
  }
RLoniello
  • 2,309
  • 2
  • 19
  • 26
0

I ended up just simply asking the user for their game controller layout. The answer from Ercell0 is a nice way to connect and use the game controllers, but doesn't really answer my question.

KingCoder11
  • 415
  • 4
  • 19