Reporting score from a different UIViewController IOS - ios

I'm not sure what is going on.
If i report an int score from the same viewcontroller, it seems to work fine. I can send that number to the leaderboard set up in game center.
When I report an int score that is from another viewcontroller, it doesn't work. At first I thought it was because the numbers weren't in int64_t. I'm not sure if that is really the issue though.
I'm fairly new to ios programming. If this is a duplicate question, please direct me in the right direction. I really appreciate any help.
-(IBAction)report:(id)sender{
[self reportScore]; //just calling the method
}
-(void)reportScore{
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];
score.value = HighScore; //HighScore is the int I want from another viewcontroller.
[GKScore reportScores:#[score] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(#"%#", [error localizedDescription]);
}
}];
}

I guess
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];
This line creating the object each time. And thats the reason you are getting the wrong result.
Try to create GKScore *score object only once which will be global and then access it.

Related

GameCenter Report Score - Local declaration hides instance variable

I'm adding Game Center functionality to my app, and I have run into something strange that I can't get my head around...
I've used this exact method (and code) in 5 games, so I can't see why it's throwing a warning message now...
I get 2x "local declaration of 'score' hides instance variable" in the ReportScore method...
The code is as follows:
-(void)reportScore{
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];
score.value = gameScore; //gameScore is games Score that needs submitting
[GKScore reportScores:#[score] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(#"%#", [error localizedDescription]);
}
}];
}
I've tried declaring the variable like this in the .h:
#property (nonatomic) GKScore *score;
But that introduces an autosynthesised warning instead... I don't understand why this is happening when it doesn't do this in any other apps of mine?
I can't believe I did this... It shows how easy it is to miss things when you're staring at code so long...
It turns out, I have a UIImageView with the name score...
.h
IBOutlet UIImageView *score;
Simply changing the GKScore variable name, the warnings disappeared...
-(void)reportScore{
GKScore *this_score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];
this_score.value = gameScore;
[GKScore reportScores:#[this_score] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(#"%#", [error localizedDescription]);
}
}];
NSLog(#"Reported to Game Center...");
}
What a doughnut!

In IOS, how to get leaderboard rank in Google Play Games SDK?

My english is little short. I attach Google Play Games to iPhone5. Everything is fine except leaderboard rank. When I attempt to get rank from leaderboard, It always return zero.
Below is my code. What is problem?
- (void)viewDidLoad {
[super viewDidLoad];
[GPGManager sharedInstance].statusDelegate = self;
}
- (IBAction)signInWasClicked:(id)sender {
[[GPGManager sharedInstance] signInWithClientID:CLIENT_ID silently:NO];
}
- (IBAction)submitScore: (UIButton *)sender {
GPGScore* myScore = [[GPGScore alloc] initWithLeaderboardId:LEADERBOARD_ID];
[myScore submitScoreWithCompletionHandler:^(GPGScoreReport *report, NSError *error){
if (error) {
// error
NSLog(#"ERROR");
} else {
// Success. Score retern fine. score return right value but rank is not.
NSLog(#"%#, %lld", report.highScoreForLocalPlayerAllTime.formattedScore,
report.highScoreForLocalPlayerAllTime.rank);
}
}];
}
In Google developer's "Leaderboard in iOS" section, there is no mention about leaderboard rank. But in GPGScoreReport object, there is GPGScore object and in GPGScore object, score and rank value are on it.
Help me please.
GPGLeaderboard *myLeaderboard = [GPGLeaderboard leaderboardWithId:LEADERBOARD_ID];
myLeaderboard.timeScope = GPGLeaderboardTimeScopeAllTime;
GPGLocalPlayerScore* localScore = [myLeaderboard localPlayerScore];
GPGLocalPlayerRank* localRank = [localScore publicRank];
NSLog(#"Current rank : %#", localRank.formattedRank);
I didn't try this code, but according to the class references, it should work fine. Let me know whether it works or not.
Also, put that code "after" submitting your score.

Send and receive invites of GKTurnBasedMatch

I am developing a board game. Using Game Center for multiplayer, but currently stuck at how to send or receive invitations of GKTurnBasedMatch. I am creating match programmatically using:
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.defaultNumberOfPlayers = 2;
[GKTurnBasedMatch findMatchForRequest:request
withCompletionHandler:
^(GKTurnBasedMatch *match, NSError *error) {
if(error) {
NSLog(#"%#", error.localizedDescription);
return;
}
[self.delegate startGameForMatch:match];
}];
The GKTurnBasedMatch instance in parameter of above block, has only local player with other player as nil and I need to display details of opponent in the game.
NSMutableArray *participantIds = [[NSMutableArray alloc] init];
NSLog(#"%#", match.participants);
for (GKTurnBasedParticipant *participant in match.participants) {
if(participant.playerID) [participantIds addObject:participant.playerID];
}
[GKPlayer loadPlayersForIdentifiers:participantIds
withCompletionHandler:^(NSArray *players, NSError *error) {
NSMutableString *string = [[NSMutableString alloc] init];
for (GKPlayer *player in players) {
[string appendFormat:#"---- Alias: %# DisplayName: %#", player.alias, player.displayName];
}
NSLog(#"%#", string);
}];
Am I missing something or Game Center works like this?
I read that participants of the match wont get invitations until that GKTurnBasedParticipant is GKTurnBasedMatch.currentParticipant but I need to display the details of opponent when game started.
Thanks for the help. Point me in correct direction.
This happens when you create a match against a random opponent. Try creating a second test user in iTunesConnect and signing into that user on a second device.
Then, send that second test player an invite to be your friend. This will allow you to more easily test your game with multiplayer features without having to wait for a random match to be found.
After the friend request is accepted, try creating a new game once more. Now, invite your 'Friend' to your game and start your turn. You will now notice that the (null) variables will - for the most part - be filled in. Something like this should now appear in your log-
GKTurnBasedParticipant 0xb9432e0 - playerID:G:123456789 status:Invited matchOutcome:None lastTurnDate:(null) timeoutDate:(null)

iOS 7 - game center leaderboard integration

- (void) reportScore: (int64_t) score forLeaderboardID: (NSString*) identifier
{
GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
scoreReporter.value = score;
scoreReporter.context = 0;
NSArray *scores = #[scoreReporter];
[GKLeaderboard reportScores:scores withCompletionHandler:^(NSError *error) {
//Do something interesting here.
}];
}
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html
On the above site, I used the above code (and the title was Reporting a score to Game Center (iOS 7)) but on the GKLeaderboard reportScores... line, I get an error saying that there is no such method. How do i fix this without using GKScore's deprecated reportScoreWithCompletionHandlerMethod?
So apple's thing had a typo. GKLeaderboard was supposed to be GKScore in the reportScores line.

Game Center score submitted, but leaderboard shows no score

I have a game that uses Game Center.
I'm playing the game, when the game ends, I send my score to GC. After that, I'm checking my score with the method below.
- (void)showLeaderboard
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
leaderboardController.category = self.currentLeaderBoard;
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardController.leaderboardDelegate = self;
UnityAppController* delegate = (UnityAppController*)[[UIApplication sharedApplication] delegate];
[[delegate rootViewController] presentViewController:leaderboardController animated:YES completion:nil];
}
}
That's just working fine, when I check it I can see my recent score on the leaderboard.
But the problem starts after. The next day, when I check it again with the showLeaderboard() method, the leaderboard says "no score". If I play the game and send my score again it's just working fine again, I can see my score! But, the next day the leaderboard says "no score" to me again! Like the leaderboard resets itself at 00.00 o'clock.. Just awkward..
Any suggestions? Thanks in advance..
Just make sure that you do the correct code for submitting the score. This is a way that I really like. Even though you will get warnings because they don't want you to use it in iOS 7, it still works. Just let me know if it doesn't work.
(IBAction)submitscoretogamecenter{
GKLocalPlayer *localplayer = [GKLocalPlayer localPlayer];
[localplayer authenticateWithCompletionHandler:^(NSError *error) {
}];
//This is the same category id you set in your itunes connect GameCenter LeaderBoard
GKScore *myScoreValue = [[[GKScore alloc] initWithCategory:#"insertCategory"] autorelease];
myScoreValue.value = scoreInt;
[myScoreValue reportScoreWithCompletionHandler:^(NSError *error){
//insert what happens after it's posted
}];
[self checkAchievements];
}

Resources