Uploading scores to google play games services leaderboards


#1

Hello. I am asking if there is a way to implement a feature that displays the player’s scores in the google play services leaderboard(android) in such a way that players can see the scores of others and how they rank. Has anyone been able to implement it? Please provide me with insight or advice. Thanks


#2

There is only one solution here: using a Cordova plugin. Its going to be a bit more tidious to build the game if you do this. And you will need some Javascript. If you are still determined to do that, continue to read:

  1. Finish everything else in your game.
  2. Set up on Google play console all your leaderboards and everything.
  3. Create a Js event somewhere for loving the player in.
  4. In that event write:
cordova.plugins.playGamesServices.auth(function() {
    // On logged in
    runtimeScene.getGame()
    .getVariables()
    .get("PlayGamesLoggedIn")
    .setNumber(1)
}, function() {
    // On not logged in
    runtimeScene.getGame()
    .getVariables()
    .get("PlayGamesLoggedIn")
    .setNumber(2);
})
  1. Only use anything using the Google play games API if global variable PlayGamesLoggedIn is = to 1
  2. Create Js events as sub events of whatever condition you have that triggers score getting or setting.
  3. To add a score to the score board do:
var data = {
    score: 10,
    leaderboardId: "board1"
};
cordova.plugins.playGamesServices.submitScoreNow(data, function (result) {
    // ‘result’ is the following object
    // {
    //      leaderboardId: string;
    //      playerId: string;
    //      formattedScore: string;
    //      newBest: boolean;
    //      rawScore: number;
    //      scoreTag: string;
    // }
    let res = runtimeScene.getVariables().get("ScoreSetResult");
    gdjs.evtTools.network.jsonToVariableStructure(JSON.stringify(result), res);
    res.getChild("error").setNumber("2");
}, function() {
    // On error
    runtimeScene.getVariables().get("ScoreSetResult").getChild("error").setNumber("1");
});
  1. Get the best score of the current player with:
var data = {
    leaderboardId: "board1"
};
cordova.plugins.playGamesServices.getPlayerScore(data, function (result) {
    // ‘result’ is the following object
    // {
    //      playerScore: number;
    // }
    let res = runtimeScene.getVariables().get("ScoreGetResult");
    gdjs.evtTools.network.jsonToVariableStructure(JSON.stringify(result), res);
    res.getChild("error").setNumber("2");
}, function() {
    // On error
    runtimeScene.getVariables().get("ScoreGetResult").getChild("error").setNumber("1");
});
  1. To show a leader board use this:
var data = {
    leaderboardId: "board1"
};
cordova.plugins.playGamesServices.showLeaderboard(leaderboardId, function () {
    // On success
}, function() {
    // On error
});
  1. To show them all:
cordova.plugins.playGamesServices.showAllLeaderboards(function () {
    // On success
}, function() {
    // On error
});
  1. Then you have everything in your code. You only need to export with the plugin. To do so, export manually for android and iOS. You will also need node Js and the Cordova CLI installed. Open a command prompt, cd to the directory you exported your game to, and type cordova plugin add https://github.com/artberri/cordova-plugin-play-games-services.git --variable APP_ID=you_app_id_here, then zip the directory and upload the whole thing to phonegap build.

And there you go! Don’t forget to replace the placeholders, and verify the code because I write it out of memory.

Examples adapted from https://github.com/artberri/cordova-plugin-play-games-service .

Have a nice day!


#3

Cheers mate. Wow, this is really helpful thank you very much. My only question is how would I create a js event from inside the engine?


#4

Hi Arthuro,

Maybe you can help me with this. After following your instructions above I tried building it with the cordova plugin from the CLI, but I got the following error:

11%20AM

I even changed the android.useAndroidX property to true in the gradle.properties file as suggested, but i still get the same error, when I try to build it.

I know the other option is to build it with phonegap but that’s discontinued and the google console wouldn’t accept the phonegap build for some reason.

Anyway, I was wondering if there is a way to address this error. (I should say that I’ve been able to build the game succesfully from the CLI without the cordova plugin. it’s only when I add the cordova plug in to the directory and then build it, that i get the error.)

I’ve searched everywhere for a solution to no avail. So now I turn to you. I hope you can help. Thanks.

Cheers,


#5

You probably have an outdated Cordova-android version. This will probably help you: https://stackoverflow.com/a/63842227


#6

Thanks for your quick reply. I tried your solution and it seemed to work but now I get a different error:

44%20PM

I’m very new to the CLI so forgive me if the error is obvious, but I honestly don’t know what I am supposed to do. Perhaps the cordova plugin for google play services is not compatible?

I appreciate you pointing me in the right direction.

Thanks


#7

That’s possible. Tbh all cordova plugins for Google play games are unmaintained and not up to date, I don’t know it will even be possible to publish the game to the play store using those plugins as they use extremely old APIs.


#8

Got it! Thanks for your help!