@Rodolphe, I just gave it a go and I think I have some code that does what you want.
In GameCenterAddOn.h add these lines:
static int reportAchievement(struct lua_State *state);
static int showAchievements(struct lua_State *state);
with the other similar lines underneath this one: @property (weak, nonatomic) CodeaViewController *codeaViewController;
In GameCenterAddOn.m add these lines:
lua_register(L, "reportAchievement", reportAchievement);
lua_register(L, "showAchievements", showAchievements);
with the other similar lines in this function: - (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L
then add this block of code:
#pragma mark - Report Achievement Progress
static int reportAchievement(struct lua_State *state) {
[gameCenterAddOnInstance reportAchievementAction:[NSString stringWithCString:lua_tostring(state, 1) encoding:NSUTF8StringEncoding] percentComplete:(int)lua_tointeger(state, 2)];
return 1;
}
- (void) reportAchievementAction: (NSString*) identifier percentComplete: (float) percent
{
GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier];
if (achievement)
{
achievement.percentComplete = percent;
[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
if (error != nil)
{
NSLog(@"Error in reporting achievements: %@", error);
}
}];
}
}
#pragma mark - Show GameCenter Achievements
static int showAchievements(struct lua_State *state) {
[gameCenterAddOnInstance showAchievementsAction];
return 1;
}
- (void) showAchievementsAction
{
GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil)
{
gameCenterController.gameCenterDelegate = self;
gameCenterController.viewState = GKGameCenterViewControllerStateAchievements;
[self.codeaViewController presentViewController: gameCenterController animated: YES completion:nil];
}
}
anywhere with the other functions (i placed it after the reportScore code)
This will give you two new lua functions reportAchievement
and showAchievements
showAchievements
should bring up the Game Center view showing the achievements.
reportAchievement
is called like so: reportAchievement(identifier, percentage)
where identifier
is the achievement identifier for the achievement you are updating, and percentage
is the percentage complete (so for example if the achievement was kill 5 bosses, and you killed one boss percentage
would be 20)
I haven’t tested this code (I don’t have an app with achievements) so let me know if it works or doesn’t.