-
Notifications
You must be signed in to change notification settings - Fork 9
Leaderboards
IGX supports leaderboards in the same way as IG. Global and connected player leaderboards are supported but not context 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.
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.
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
});
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
});
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
});
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.
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
