The below implementations was replaced by Native Extension. Please scroll down to see recent updates.
Code example for Game Center extension :
https://github.com/vinhvd/defoldgamecenter
Hello,
I’ve tried to build a custom leaderboard for game center :
1. Authenticate :
1.1 Lua :
> local f = assert(package.loadlib(“libgamecenter_authenticate.dylib”, “exec”))
> f()
1.2 Object C :
> #define EXPORT __attribute__((visibility("default")))
> NSString *const PresentAuthenticationViewController = @"present_authentication_view_controller";
> EXPORT
> int exec(lua_State *L) {
>
> GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
> localPlayer.authenticateHandler =
> ^(NSViewController *viewController, NSError *error) {
> if (error) {
> printf("Game Center : authenticate error : %s",
> [[[error userInfo] description] UTF8String]);
> }
>
> if(viewController != nil) {
> [[NSNotificationCenter defaultCenter]
> postNotificationName:PresentAuthenticationViewController
> object:nil];
> } else if([GKLocalPlayer localPlayer].isAuthenticated) {
> printf("Game Center : authenticate : OK");
> }
> };
>
> return 0;
>}
2. Load Scores :
2.1 Lua :
local f = assert(package.loadlib(“libgamecenter_load_scores.dylib”, “exec”))
f()
2.2 Object C :
>#define EXPORT attribute((visibility(“default”)))
>#define kLeaderboardID @"highscore"
> EXPORT
> int exec(lua_State *L) {
>
> GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
> if (leaderboardRequest != nil)
> {
> leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
> leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
> leaderboardRequest.identifier = kLeaderboardID;
> leaderboardRequest.range = NSMakeRange(1,100);
> [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
> if (error != nil)
> {
> printf("Game Center : load score error : %s",
> [[[error userInfo] description] UTF8String]);
> NSLog(@"Game Center : load score error : %@:", [[error userInfo] description]);
> }
> if (scores != nil)
> {
> GKScore* myScore = leaderboardRequest.localPlayerScore;
>
> NSLog(@"Me: %@: %d", myScore.player.alias, (int)myScore.value);
> printf("Me: %s", [myScore.player.alias UTF8String]);
>
>
> for (GKScore* score in scores)
> {
> NSLog(@"Player %@: %d", score.player.alias, (int)score.value);
> }
>
> }
> }];
> }
> return 0;
> }
3 Report Score :
3.1 Lua :
> local f = assert(package.loadlib("libgamecenter_report_score.dylib", "exec"))
> f()
3.2 Object C :
>EXPORT
> int exec(lua_State *L) {
> GKScore* highScore = [[GKScore alloc] initWithLeaderboardIdentifier:kLeaderboardID];
> highScore.value = (int64_t)64;
> [GKScore reportScores:@[highScore] withCompletionHandler:^(NSError *error) {
> if (error) {
> NSLog(@"Game Centre : report score : %@", error);
> printf("Game Center : report score : %s",
> [[[error userInfo] description] UTF8String]);
> }
> }];
> return 0;
> }
Result :
#1 Authenticate : work perfectly
Not login yet
Logged in already
#2 Load Scores :
Me and all players are enable to load in Object C codes
#3 Report Score :
It is able to store hard code score to Game Center .
Question :
As Lua standard, it is able to pass value from Lua to C and return value from C to Lua ( L is Lua state)
For example : In lua, I want to pass a score value to C as : f(1000)
then I can get the value in C to report score as :
> if(lua_isnumber(L, 1)) {
> int score = lua_tonumber(L, 1);
> }
and I can load the score from C to lua as : lua_pushnumber(L, score);
However , when pass and return the values , the game halts .
Is it possible to pass and return values in embedded Lua of Defold like the above ?
> lua_CFunction
> typedef int (*lua_CFunction) (lua_State *L);
> static int foo (lua_State *L) {
> int n = lua_gettop(L); /* number of arguments */
> lua_Number sum = 0;
> int i;
> for (i = 1; i <= n; i++) {
> if (!lua_isnumber(L, i)) {
> lua_pushstring(L, "incorrect argument");
> lua_error(L);
> }
> sum += lua_tonumber(L, i);
> }
> lua_pushnumber(L, sum/n); /* first result */
> lua_pushnumber(L, sum); /* second result */
> return 2; /* number of results */
> }
Thanks,
-RG1