Skip to content

Leaderboards

Mat Hopwood edited this page Jun 26, 2019 · 11 revisions

IGX supports leaderboards in the same way as IG. Global and connected player leaderboards are supported but not context leaderboards.

Setting up Leaderboards

The leaderboard service is created in the same way as other services, e.g:

new LeaderboardsService("xtralife");

Note that if you called FBInstant.createDefaultServices() to create the default services then you should not recreate the leaderboards service module. Also note that when using the Xtralife back-end leaderboards are automatically created, you do not need to create them on the back-end.

Submitting a Leaderboard Score

Below is an example that shows how to submit a score:

FBInstant.getLeaderboardAsync(leaderboard_name, options)
    .then(function(leaderboard) {
        return leaderboard.setScoreAsync(score, meta);
    }).then(function(entry) {
        // Score saved, entry contains a LeaderboardsService.LbdEntry
    }).catch(function(error) {
        // Error saving score
    });

options will contain various options that are passed to whichever back-end you use, In the case of Xtralife it supports a single option called sortOrder. This can be hightolow or lowtohigh depending on what type of score sorting you require.

Retrieving a Leaderboard Score

Below is an example that shows how to retrieve a score:

FBInstant.getLeaderboardAsync(leaderboard_name)
    .then(function(leaderboard)
    {
        return leaderboard.getPlayerEntryAsync();
    }).then(function(entry) {
        // entry contains a LeaderboardsService.LbdEntry
    }).catch(function(error) {
        // Error retrieving score
    });

Retrieving a Global Leaderboard

Below is an example that shows how to retrieve a leaderboard:

FBInstant.getLeaderboardAsync(leaderboard_name)
    .then(function(leaderboard) {
        return leaderboard.getEntriesAsync(count, start);
    }).then(function(entries) {
        // entries contains a collection of LeaderboardsService.LbdEntry
    }).catch(function(error) {
        // Error retrieving leaderboard
    });

Retrieving a Connected Player Leaderboard

Below is an example that shows how to retrieve a leaderboard:

FBInstant.getLeaderboardAsync(leaderboard_name)
    .then(function(leaderboard) {
        return leaderboard.getConnectedPlayerEntriesAsync(count, start);
    }).then(function(entries) {
        // entries contains a collection of connected player LeaderboardsService.LbdEntry
    }).catch(function(error) {
        // Error retrieving leaderboard
    });

Collecting New Leaderboard Data

Its worth thinking about your user and the server when collecting new leaderboard information. Its not generally wise to collect fresh leaderboard inside of a loop because it will hammer both the user and the server. Try to only collect new leaderboard data every few minutes.

Showing leaderboards

Some vendors allow you to show leaderboards using their own UI or native UI, for example Unity allows you to show Google Play Games and Game Centre leaderboards. This functionality is available via an extension function:

FBInstant.ext.showLeaderboard(options); // option is currently unused

Help keep this project alive by donating paypal

Clone this wiki locally