-1

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.

1 Answers1

1

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!!

  • So what's the rest of the solution besides upgrading Xcode and tvOS to the latest version? – HangarRash Apr 05 '23 at 16:10
  • The rest has zero to do with NotificationCenter. It has to do with calling another of my funcs from within a **@obj func**. Specifically accessing one of my vars within the C func. –  Apr 05 '23 at 17:19
  • My point is that your answer doesn't really provide an answer. It doesn't explain what the actual solution is. You've only stated that you got it solved and that updating some software was a significant (but not all) part of the solution. Please update your answer explaining the complete solution so it can help future readers. – HangarRash Apr 05 '23 at 17:21
  • My OP addressed CONNECTION status, ie, NotificationCenter. I stated this issue was addressed by **major** updates to Xcode and tvOS. These are not **some** updates. End of discussion. Now and forever. –  Apr 05 '23 at 17:28
  • You stated *"A significant part of the solution rests with..."* - this implies there is still some other part of the solution besides updating the software. Your comments seem to indicate that updating the software is all that is needed. If that's true, your answer should make that clear. – HangarRash Apr 05 '23 at 17:32