Of course my friend...
UntitledViewController.h
First of all we add this 2 lines to the top...
#import <GameKit/GameKit.h>
#import "GameCenterManager.h"
after added the GameCenter class header we can start to declare it...
so we add before the interface:
@class GameCenterManager;
and we initialize it in the interface...
GameCenterManager *gameCenterManager;
we need also to add the property for the class...
@property (nonatomic, retain) GameCenterManager *gameCenterManager;
and than declare the functions (called "method" in obj c) for the .m file
- (void) submitScore: (int64_t) _score;
- (void) showLeaderboard;
Why i added the _score variable?
We don't need to do much turn around, we can go straight to our objective, so we can pass directly our C++ player points in a INT format (or in a format you want)
UntitledViewController.m
Here we need to add the new methods to the implementation, and is easy like in C++
But to get full features of the UntitledViewController we need to #import the Core.h
after we have to add a new synthesize of the GameManager class we added in .h
@synthesize gameCenterManager;
i also added this method from the GKTapper example cause AppGameKit don't have the message with title so the first method to add is this:
- (void) showAlertWithTitle: (NSString*) title message: (NSString*) message
{
UIAlertView* alert= [[[UIAlertView alloc] initWithTitle: title message: message delegate: NULL cancelButtonTitle: @"OK" otherButtonTitles: NULL] autorelease];
[alert show];
}
it will be very usefull...
After this i added the viewDidLoad that is not present in template...
- (void)viewDidLoad
{
[super viewDidLoad];
if([GameCenterManager isGameCenterAvailable])
{
self.gameCenterManager = [[[GameCenterManager alloc] init] autorelease];
[self.gameCenterManager setDelegate: self];
[self.gameCenterManager authenticateLocalUser];
}
else
{
[self showAlertWithTitle: @"Game Center Support Required!"
message: @"The current device does not support Game Center."];
}
}
This will autoinit the GameCenter class on the load of first view.
Finally i add the 2 methods to submit scores and load the leaderboard and the controller on close...
-(void)submitScore: (int64_t) _score
{
//This is the same category id you set in your itunes connect GameCenter LeaderBoard
GKScore *myScoreValue = [[[GKScore alloc] initWithCategory:@"YourGameIDLeaderBoard"] autorelease];
myScoreValue.value = _score;
[myScoreValue reportScoreWithCompletionHandler:^(NSError *error){
if(error != nil){
[self showAlertWithTitle:@"HighScore" message:@"Score saved!"];
} else {
[self showAlertWithTitle:@"HighScore" message:@"Score not saved!"];
}
}];
}
- (void)showLeaderboard
{
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != NULL)
{
leaderboardController.category = @"YourGameIDNameLeaderBoard";
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
//leaderboardController.modalPresentationStyle = UIModalPresentationFullScreen;
leaderboardController.leaderboardDelegate = self;
iphone_appAppDelegate *appDelegate = (iphone_appAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.viewController presentModalViewController:leaderboardController animated:YES];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
iphone_appAppDelegate *appDelegate = (iphone_appAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.viewController dismissModalViewControllerAnimated:YES];
}
Now the last thing to add is a declaration in the cpp...
UntitledViewController *g_View = [UntitledViewController alloc];
so we have a g_View variable where to pass the methods and variables.
I also added this to the .h file of cpp to be sure to have all features available in all files...
#import "UntitledViewController.h"
#import "Core.h"
Now done this, we have finished.
To call the leaderboard we use: [g_View showLeaderboard];
To submit a score we use : [g_View submitScore:myvariable_points];
This workflow maybe will open your eyes on what we can do together with AppGameKit and Obj-C...
We can do very much things.
For example the showAlertWithTitle shows you how to use this method instead of agk::message.
Well AppGameKit staff surely will add it in next releases but in the while we wait we can mix C++/Obj C in an easy way.
The same process can be used also to integrate the inAppBuy and all the other features of the GameCenter. We finally don't need to add new views or subviews (until we really need them).
In the same way i'm trying to use buttons and menu from Obj-C and mixing to the AGK.
And i think will work very well. As the Input text is not usefull on AppGameKit, we can do it using the same way we are using for the game center.
Sorry for my rude english
iPhone/iPad Games : Street Karate Fighter - 90s VideoPokers Simulator - Arkanold