Using Swift, how do I continuously test for a GamePad being on/connected?
When you build a Browser-based Canvas + Javascript Game, you do the above via:
function listenForGamepadConnected() {
window.addEventListener("gamepadconnected", (event) => {
// this is the BIGEE that is always testing
});
} // listenForGamepadConnected
And it definitely works!
I cannot find its equivalent in the Xcode/Swift world?
I do see the following that’s built into a boilerplate Game that you initially create using Xcode - but it seems that it just looks once, versus continuously:
func ObserveForGameControllers() {
NotificationCenter.default.addObserver(
self,
selector: #selector(connectControllers),
name: NSNotification.Name.GCControllerDidConnect,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(disconnectControllers),
name: NSNotification.Name.GCControllerDidDisconnect,
object: nil)
} // ObserveForGameControllers
@objc func connectControllers() {
// Unpause the Game if it is currently paused
self.isPaused = false
// Used to register the Nimbus Controllers to a specific Player Number
var indexNumber = 0
// Run through each controller currently connected to the system
for controller in GCController.controllers() {
// Check to see whether it is an extended Game Controller (Such as a Nimbus)
if controller.extendedGamepad != nil {
print("CONNECTED - Extended Gamepad #\(indexNumber)")
controller.playerIndex = GCControllerPlayerIndex.init(rawValue: indexNumber)!
indexNumber += 1
setupControllerControls(controller: controller)
}
else {
print("CONNECTED - but, NOT an Extended Gamepad #\(indexNumber)")
}
}
} // connectControllers
@objc func disconnectControllers() {
print("DIS-CONNECTED")
// Pause the Game if a controller is disconnected ~ This is mandated by Apple
self.isPaused = true
} // disconnectControllers
I try to call it in a Timer loop, but still does not work:
@objc func testForGamepadIsConnected() {
ObserveForGameControllers
var gamepadOn = !self.isPaused // does not work
} // testForGamepadIsConnected
func startTestForGamepad() {
guard (gamepadTimer != nil) else {
gamepadTimer = Timer.scheduledTimer(
timeInterval: 1.0,
target: self,
selector:#selector(testForGamepadIsConnected),
userInfo: nil,
repeats: true)
return
}
} // startTestForGamepad
func stopTestForGamepad() {
guard (gamepadTimer == nil) else {
gamepadTimer!.invalidate()
// .invalidate() removes Timer() from gamepadTimer, so reinitialize it.
gamepadTimer = Timer()
return
}
} // stopTestForGamepad
Scoured the Google world, but I’ve come up empty.
Would appreciate some genius out there providing what I’m missing.
Thanks loads.
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!!
Thank you Justin for not throwing a brick at me.