Search code examples
swiftgamecontroller

How to continuously test for the connection of a Game Controller?


How to continuously test for the connection of a Game Controller?

Stepping back from code just for a moment ...

What do I want to do?

1) upon initial start up of app, I want to wait for a gamepadconnected Event.

2) Once I get this Event by turning on my Game Controller, I immediately change a SKSpriteNode's color and then start looking for:

a) button presses on the gamepad and

b) a gamepaddisconnected Event

3) So while I am executing code based on 2(a), I receive 2(b) when I turn off my Game Controller. I immediately change the above color to something different, followed by looking for another gamepadconnected Event.

Back-and-forth.

Back-and-forth.

I have tried using Apple's very own NotificationCenter Code which works except for a significant omission, namely, their Code never triggers a gamepaddisconnected Event as determined by my placement of multiple print statements.

== start what I have done so far ==

I now call Apple's very own ObserveForGameControllers() within my GameViewController's viewDidLoad().

My Nimbus+ Game Controller is set to off.

I initially Run my Project, Apple's Code consistently shows "CONNECTED".

Furthermore, Apple states that their two NotificationCenter.default.addObserver() @objc funcs are continuously called.

Here's Apple's code:

func ObserveForGameControllers() {    
    NotificationCenter.default.addObserver(
                self,
                selector: #selector(connectControllers),
                name: NSNotification.Name.GCControllerDidConnect,
                object: nil)   // means <Any> Object can send Message
    
    NotificationCenter.default.addObserver(
                self,
                selector: #selector(disconnectControllers),
                name: NSNotification.Name.GCControllerDidDisconnect,
                object: nil)
            
}   // ObserveForGameControllers

Apple's connectControllers selector starts the action by waiting for the user to press a Button before doing anything else.

If a Button is pressed then code is executed, e.g., moving a Game Piece in a specific direction.

Now, if for any reason, I disconnect the Game Controller while the Game is running, e.g., via pressing its Home Button, I want to call my stopGame() to do certain things, like changing colors as specified above. This call to stopGame() is placed in my disconnectControllers selector.

But, it's never called!

I should be able to toggle my Nimbus+ off and on while playing ...

Since Apple states that these two addObserver() @objc funcs are cotinuously called. I deliberately have print statements in each @objc.

With this toggling is going on, there should be a whole slew of print statements that toggle between "connected" and "dis-connected".

Never happens!!

== end what I have done so far ==

Clearly, I am open to use alternatives to Apple's NotificationCenter code.

Nevertheless, I really would prefer to stick with the latter.

I just wish I knew how to correctly implement it.

Note: some have blamed the Xcode Simulator because it is obviously not a real device.

Well, I have installed my App on my Apple TV and the same challenges persist.


Solution

  • SOLVED --

    1st and foremost, I want to sing the praises of Keith at Apple DTS without whom I could never have chased down the cause of my problem. His assistance on this one issue totally pays for my $99.

    A significant part of the solution rests with the just released update of Xcode to 14.3 and tvOS to 16.4

    Now the expected CONNECTED and DIS-CONNECTED Console messages appear as anticipated.

    I've got some more work to do, but at least Apple's

    NotificationCenter.default.addObserver(..)

    works as Apple advertises.

    WHOOPIE!!