Swift Macos GameController not showing. Controller.count return zero

268 views Asked by At

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

There are 1 answers

2
Pablo Jiménez Revilla On

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