0

I'm developing an app which includes Gamecontroller. I'm trying to show the connected controller count, but it´s not working.

Here is my simple code:

import Cocoa
import GameController

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        // - MARK: Controllers
         
        let controllers = GCController.controllers()
        print(controllers.count)
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

Controller is connected

it always prints 0

What am I doing wrong?

EDIT:

The problem was i mised to add:

   let ctr = NotificationCenter.default
   ctr.removeObserver(self, name: .GCControllerDidConnect, object: nil)
   ctr.removeObserver(self, name: .GCControllerDidDisconnect, object: nil)

To handle when a controller is connected

1 Answers1

1

Fixed using this tutorial: tvOS Games, Part 1: Using the Game Controller Framework

The next problem is to find a unique identifier for the device...

EDIT:

The problem was i mised to add:

let ctr = NotificationCenter.default
   ctr.removeObserver(self, name: .GCControllerDidConnect, object: nil)
   ctr.removeObserver(self, name: .GCControllerDidDisconnect, object: nil)

To handle when a controller is connected

  • 2
    Hey Pablo, could you clarify exactly what your problem was, and what step in that tutorial fixed it? Without that, this Q&A won't be of much use to future readers, who might be struggling with the same problem you had. Also, websites go down and links go dead, so it's always good to summarize linked resources directly in the answer. The linked resource should be like an addendum, and not a core part of the answer – Alexander Feb 07 '22 at 13:25
  • Edied with the answer – Pablo Jiménez Revilla Feb 08 '22 at 13:25