You already know that Hog Dice doesn’t use the Game Center Access Point. However, players will still need a way to access the Game Center Dashboard because what’s the point of adding Game Center features if players can’t access them, right?
In Hog Dice, you’ll provide a way for players to access the dashboard, leaderboards, and achievements using the following three buttons:
These three buttons are the main access points into the Game Center. The first one is located in the lobby scene, while the other two are located in the game over scene.
The first step is to create another shared view controller. Switch to the GameKitHelper.swift file and add a new property:
| var gameCenterViewController: GKGameCenterViewController? |
The GKGameCenterViewController class is the single user interface that displays the Game Center information.
As you did with the authentication controller, you’ll use a notification to present the controller. In the Notification.Name extension, add the following code:
| static let presentGameCenterViewController = |
| Notification.Name("presentGameCenterViewController") |
However, unlike the authentication controller, you also need to implement the GKGameCenterControllerDelegate protocol, which handles dismissing the Game Center controller. At the bottom of the GameKitHelper.swift file, just above the Notification.Name extension mark, add the following code:
| // MARK: - DELEGATE EXTENSIONS |
| |
| extension GameKitHelper: GKGameCenterControllerDelegate { |
| func gameCenterViewControllerDidFinish(_ gameCenterViewController: |
| GKGameCenterViewController) { |
| gameCenterViewController.dismiss(animated: true, completion: nil) |
| } |
| } |
Finally, add the method that handles showing the Game Center view controller, placing it in the extension you just created:
| // Show the Game Center View Controller |
| func showGKGameCenter(state: GKGameCenterViewControllerState) { |
| guard GKLocalPlayer.local.isAuthenticated else { return } |
| |
| // Prepare for new controller |
| gameCenterViewController = nil |
| |
| // Create the instance of the controller |
| if #available(iOS 14, *) { |
| gameCenterViewController = GKGameCenterViewController(state: state) |
| } else { |
| gameCenterViewController = GKGameCenterViewController() |
| gameCenterViewController?.viewState = state |
| } |
| |
| // Set the delegate |
| gameCenterViewController?.gameCenterDelegate = self |
| |
| // Post the notification |
| NotificationCenter.default.post(name: .presentGameCenterViewController, |
| object: self) |
| } |
Excellent, it’s time to update the GameViewController class to handle presenting the Game Center view controller.
Switch to the GameViewController.swift file. At the bottom of the file, still inside the class, add the following code:
| @objc func showGameCenterViewController() { |
| if let viewController = GameKitHelper.shared.gameCenterViewController { |
| present(viewController, animated: true, completion: nil) |
| } |
| } |
Then, in the viewDidLoad() method, add the observer, placing it below the one you created earlier:
| NotificationCenter.default.addObserver( |
| self, |
| selector: #selector(self.showGameCenterViewController), |
| name: .presentGameCenterViewController, object: nil) |
The final piece to this puzzle is to update the button actions in the lobby and game over scenes.
Switch to the LobbyScene.swift file, and in the touchDown(atPoint:) method, replace the line that reads // TODO: Add code to open Game Center with the following one-liner:
| GameKitHelper.shared.showGKGameCenter(state: .dashboard) |
This code calls the showGKGameCenter(state:) method, passing in the GKGameCenterViewControllerState value that represents the Game Center dashboard.
Now, head over to the GameOverScene.swift file, and in its touchDown(atPoint:) method, replace the line that reads // TODO: Add code to open Leaderboards, with the following:
| GameKitHelper.shared.showGKGameCenter(state: .leaderboards) |
Also, replace the line that reads // TODO: Add code to open Achievements with this one:
| GameKitHelper.shared.showGKGameCenter(state: .achievements) |
These two lines of code open the Game Center in their respective states.
Build and run the project. On the lobby scene, tap the Game Center button and you’ll see the standard Game Center dashboard:
To test the game over scene buttons, tap either the chips (on the left) or the dice (on the right) of the lobby scene. After the scene loads, tap the Leaderboards button to open the Game Center Leaderboards view:
Close that view and then tap the Achievements button to open the Game Center Achievements view:
Excellent, the Game Center integration is working quite nicely, which means you’re ready to add the code that reports the player’s wins.