[DEPRECATED] Various Codea Objective-C Libraries

@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.

Oh my… oh my… I can’t believe it… I’ll get back to you with results :slight_smile:

it doesn’t seem to work right now. I call reportAchievement as such:

if h ~= nil and h[2] >= 150 and level < 1 then -- If the height of 150m is achieved and the first achievement hasn't been awarded yet
    saveLocalData("level", 1)
    level = readLocalData("level",0)
    reportAchievement("beinnbheigeir", 100) -- award achievement "Beinn Bheigeir"
end

where the achievement I want to award is:

https://www.dropbox.com/s/n2nxh67clrqj1fy/Capture%20d'écran%202014-07-31%2015.22.08.png

@Rodolphe, I’ll take a look. How long ago did you register the achievement?

A few hours, at the same time as the leaderborad, which works. Something else: calling showAchievements() works as expected !

Oops. I made a typo. it isn’t passing the percentage, it’s passing the identifier for both variables… fixed above.

replace this line [gameCenterAddOnInstance reportAchievementAction:[NSString stringWithCString:lua_tostring(state, 1) encoding:NSUTF8StringEncoding] percentComplete:(int)lua_tointeger(state, 1)];

with this: [gameCenterAddOnInstance reportAchievementAction:[NSString stringWithCString:lua_tostring(state, 1) encoding:NSUTF8StringEncoding] percentComplete:(int)lua_tointeger(state, 2)];

hopefully that fixes it for you.

it wooooorks!!! All of this is quite amazing, I must say… thanks so much

@Rodolphe, glad it works :slight_smile:

Another question already… :smiley:

I get get an error “attempt to call global ‘sendTweet’ (a nil value)” when using:

sendTweet("hello, hello")

Is this what you, @Zoyt, means when you write “Right now my sharing API only lets you share photos, but that’ll change”?

@Rodolphe, that means that the function sendTweet does not exist.

I believe his addon supports the functions tweetPhoto and facebookPhoto only.

Thanks @JakAttak for answering those questions. You’re the new Obj-C guru here. :wink:
Anyways, @Rodolphe, are you sure you included the add on completely?
Make sure you have @property (strong, nonatomic) SharingAddOn *sharingAddOn; in AppDelegate.h and self.sharingAddOn = [[SharingAddOn alloc] init]; [self.viewController registerAddon:self.sharingAddOn]; in AppDelegate.mm.
Edit: Also, make sure you’re using the one from my StackIt repository, not my snippets off GitHub.
Edit #2: To attach an image to the Tweet/FaceBook post, just add a second argument with the image.

@Zoyt or @JakAttak. Can you explain how to implement the Lua part of the IAP? I have the iTunes Connect part ready, but I’m not sure which functions I call when a user wants to disable ads. Thanks!

I suggest having a look through the Stack It source code and looking at how Zoyt implemented it.

@JakAttak, wow. I just got done reading through parts of the code. I think I’ll save implementing IAPs for some other time. Seems like too much work especially when I have read about your posts about how not many people buy IAPs

@YoloSwag - It’s actually quite simple, considering how much code has to go into the Obj-C side. But yes, it’s not the first thing you should worry about when writing an app.

I’m terrible at objective c, sorry to say it but I can’t help you, if it’s in Lua 5.1 or batch then, yes, but other than that, no

@Zoyt. I am having trouble repositioning the game center banner in landscape mode. The banner works great on the iPhone portrait and iPad portrait view, but on the iPad landscape view its too much to the right.

This is the code for the banner:

static int showGCBanner(struct lua_State *state) {
    [gameCenterAddOnInstance showGCBannerAction:[NSString stringWithCString:lua_tostring(state, 1) encoding:NSUTF8StringEncoding] withMessage:[NSString stringWithCString:lua_tostring(state, 2) encoding:NSUTF8StringEncoding]];
    
    return 0;
}

- (void) showGCBannerAction : (NSString*) title withMessage: (NSString*) message
{
    [GKNotificationBanner showBannerWithTitle: title message: message
                            completionHandler:nil];
}

@YoloSwag - It’s probably a bug in iOS. If you don’t mind sharing a screenshot, that’d be great.
There’s really no way to fix it, unless you found a way to isolate and snapshot the notification banner or managed to get a reference to it. Sorry.

@Zoyt. How do you add pictures from your camera roll here?

@YoloSwag - http://daringfireball.net/projects/markdown/syntax#img