Reliable gaming center

In each example of the presentation of achievements in the Game Center, I see this code

[achievement reportAchievementWithCompletionHandler:^(NSError *error) { if (error != nil) { // Retain the achievement object and try again later (not shown). } }]; 

The problem is that one small comment is about 99% of the work. I spent the last few hours trying to figure it out, and it seems like it's an endless set of extreme cases of sending and re-sending and saving and loading data.

Does anyone know a good tutorial (or sample code) that actually explains the hard part?

It is not as simple as saving them to a file and downloading them later. You have problems when you start saving several achievements and sending them later, and then they all return unsuccessfully (in blocks!), And you need to save them again ... quickly / safely ... because the application can exit and you don't want to lose them.

I am pulling my hair out.

+4
source share
1 answer

I don’t think you need to pull your hair out.

I think the basic model is this:

(a) Regardless of the Game Center, your game has a saved state (which you always need to restore the player to where he was when the game ends / in the background). This state should include all the normal things necessary to restore the state of the game, as well as include flags for all achievements in your game.

(b) Game Center also saves all achievements in your game. When you connect to the Game Center, you download the achievements made by the login members. (There is a question about who is logging into the account, but your question is not about that, so let me assume that "one true player" is always a game center player registered in the game.)

(c) Whenever a player makes an achievement, first update the corresponding achievement flag in the saved states of your game. Secondly, try to tell Game Center about this achievement. If this works, update your copy of the achievements of the game centers with new data.

(d) If it does not work, you have an achievement noted in your permanent state that is not marked in your copy of the Game Center status. At different convenient times (for example, when another achievement is won, when you finish the level, when your application starts, when you close the application, etc.), check if there are any discrepancies between what is in your own state , and what is in the state of the game center. Discrepancy is an update that must be submitted to Game Center. Try sending, but if it doesn’t work, just wait until the next opportunity appears (for example, when another achievement is reached, when you finish the level, when your application starts, when you open the application, etc.), to try again . You will never lose data because it is in your local, true picture of the user's state. (The only way you lost it is for the user to uninstall your application before you can successfully get it in Game Center, but then what can you do?)

+6
source

Source: https://habr.com/ru/post/1369354/


All Articles