简体   繁体   中英

iOS Game Center submit float instead of int64_t

I am trying to submit a float of two decimal length to my Game Center leaderboard, however the only format allowed to submit with is int64_t . I am using the default Apple report score method:

- (void)reportScore:(int64_t)score forCategory:(NSString *)category {
    GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];   
    scoreReporter.value = score;
    [scoreReporter reportScoreWithCompletionHandler: ^(NSError *error) {
        [self callDelegateOnMainThread: @selector(scoreReported:) withArg: NULL error: error];
    }];
}

I am trying to use this method to provide the score to the report score method:

- (IBAction)increaseScore {
    self.currentScore = self.currentScore + 1;
    currentScoreLabel.text = [NSString stringWithFormat: @"%lld", self.currentScore];
    NSLog(@"%lld", self.currentScore);
}

Please help, I have been googling like crazy and cannot find the answer to this.

GameCenter only accepts int64_t

The only difference between values that appear like floats or decimal values and those that appear as integers is the position of the decimal mark, while in fact all of them are int64_t.

If your internal representation is a double and you configured game center to show 3 digits after the decimal mark you have to convert it to an integer by multiplying with 10^3 and casting to integer.

int64_t gameCenterScore = (int64_t)(doubleValue * 1000.0f)

You can only submit 64 bit integers as scores to a leaderboard. From the documentation :

To Game Center, a score is just a 64-bit integer value reported by your application. You are free to decide what a score means, and how your application calculates it. When you are ready to add the leaderboard to your application, you configure leaderboards on iTunes Connect to tell Game Center how a score should be formatted and displayed to the player. Further, you provide localized strings so that the scores can be displayed correctly in different languages. A key advantage of configuring leaderboards in iTunes Connect is that the Game Center application can show your game's scores without you having to write any code.

That doc page should tell you about formatting your score. It sounds like in order to display float-like scores you will have to tinker with the format settings in iTunes Connect.

Update

Try this for increaseScore:

- (IBAction) increaseScore {      
     self.currentScore = self.currentScore + 5; 
     float score = (float)self.currentScore / 100.0f;
     currentScoreLabel.text = [NSString stringWithFormat: @"%f", score]; 
     NSLog(@"%lld", self.currentScore);
}

You can see the GKScore.h file.

@property(nonatomic, assign)            int64_t     value;              // The score value as a 64bit integer.

So float value now is not available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM