Difference between revisions of "FBInstant.getLeaderboardAsync"
From GiderosMobile
Line 8: | Line 8: | ||
FBInstant.getLeaderboardAsync(name,callback) | FBInstant.getLeaderboardAsync(name,callback) | ||
</source> | </source> | ||
− | '''name''': (string) The name of the leaderboard. Each leaderboard for an Instant Game must have its own distinct name. | + | === Parameters === |
− | '''callback''': (function) A function that will be called with two arguments: Result with the matching leaderboard, rejecting if one is not found. An error code if the function failed. ''''''<br/> | + | '''name''': (string) The name of the leaderboard. Each leaderboard for an Instant Game must have its own distinct name. <br/> |
+ | '''callback''': (function) A function that will be called with two arguments: Result with the matching leaderboard, rejecting if one is not found. An error code if the function failed. <br/> | ||
+ | === Examples === | ||
+ | '''Example'''<br/> | ||
+ | <source lang="lua"><br /> | ||
+ | FBInstant.getLeaderboardAsync("my_aweome_leaderboard", function(result,error) | ||
+ | print("getLeaderboardAsync result:",result) | ||
+ | end) | ||
+ | <br/></source> | ||
+ | '''Example 2'''<br/> | ||
+ | <source lang="lua"><br /> | ||
+ | FBInstant.getLeaderboardAsync("my_aweome_leaderboard", function(result,error) | ||
+ | print("name",result:getName()) -- should display 'my__awesome_leaderboard' | ||
+ | print("context id",result:getContextID()) -- eg 12345678 or null if not tied to a context | ||
+ | result:getEntryCountAsync( function (result,error) -- Fetches the total number of player entries in the leaderboard. | ||
+ | if result then | ||
+ | print("Count",result) | ||
+ | end | ||
+ | end) | ||
+ | |||
+ | --Update the player's score. If the player has an existing score, the old score will only be replaced if the new score is better than it. | ||
+ | -- NOTE: If the leaderboard is associated with a specific context, the game must be in that context to set a score for the player. | ||
+ | result:setScoreAsync(level,FBInstant.player.getName(),function(self,result,entry) | ||
+ | if result then | ||
+ | print("score ok",entry) | ||
+ | for key,val in pairs(entry.entry) do | ||
+ | print(key,val) | ||
+ | end | ||
+ | end | ||
+ | end) | ||
+ | |||
+ | -- Retrieves the leaderboard's entry for the current player, or null if the player has not set one yet. | ||
+ | result:getPlayerEntryAsync( function (self,result,entry) | ||
+ | if entry then | ||
+ | for key,val in pairs(entry.entry) do | ||
+ | print(key,val) | ||
+ | if key=="player" then | ||
+ | for key2,val2 in pairs(val) do | ||
+ | print(" ",key2,val2) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end) | ||
+ | |||
+ | -- Retrieve a set of leaderboard entries, ordered by score ranking in the leaderboard. | ||
+ | -- parameter: count number The number of entries to attempt to fetch from the leaderboard. Defaults to 10 if not specified. Up to a maximum of 100 entries may be fetched per query. | ||
+ | -- parameter: offset number The offset from the top of the leaderboard that entries will be fetched from. | ||
+ | result:getEntriesAsync(10,0,function(self,result,entries) | ||
+ | print(self,result,entries) | ||
+ | for loop=1,#entries do | ||
+ | local e=entries[loop].entry | ||
+ | print("at entry",loop) | ||
+ | for key,val in pairs(e) do | ||
+ | print(key,val) | ||
+ | if key=="player" then | ||
+ | for key2,val2 in pairs(val) do | ||
+ | print(" ",key2,val2) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end) | ||
+ | |||
+ | -- Most useful of all: | ||
+ | |||
+ | -- Retrieve a set of connected player leaderboard entries, ordered by score ranking in the leaderboard. | ||
+ | -- parameter: count number The number of entries to attempt to fetch from the leaderboard. Defaults to 10 if not specified. Up to a maximum of 100 entries may be fetched per query. | ||
+ | -- parameter: offset number The offset from the top of the leaderboard that entries will be fetched from. | ||
+ | result:getConnectedPlayerEntriesAsync(10,0,function(self,result,entries) | ||
+ | print(self,result,entries) | ||
+ | for loop=1,#entries do | ||
+ | local e=entries[loop].entry | ||
+ | print("at entry",loop) | ||
+ | for key,val in pairs(e) do | ||
+ | print(key,val) | ||
+ | if key=="player" then | ||
+ | for key2,val2 in pairs(val) do | ||
+ | print(" ",key2,val2) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end) | ||
+ | |||
+ | end) | ||
+ | <br/></source> | ||
+ | '''Example 3 - global leaderboard'''<br/> | ||
+ | <source lang="lua"><br /> | ||
+ | FBInstant.getLeaderboardAsync("my_aweome_leaderboard",function (e,error) | ||
+ | print("global leaderboard event",e) | ||
+ | if e then | ||
+ | for key,value in pairs(e) do | ||
+ | print(key,value) | ||
+ | end | ||
+ | else | ||
+ | print("global leaderboard error",error) | ||
+ | end | ||
+ | end) | ||
+ | <br/></source> | ||
+ | '''Example 4 - group leaderboard'''<br/> | ||
+ | <source lang="lua"><br /> | ||
+ | fbContextId=FBInstant.context.getID() | ||
+ | if fbContextId~="null" then | ||
+ | print("context",fbContextId) | ||
+ | FBInstant.getLeaderboardAsync("Highest Level Achievers."..fbContextId,function (e,error) | ||
+ | print("group leaderboard event",e) | ||
+ | print("group leaderboard error",error) | ||
+ | end) | ||
+ | else | ||
+ | print("You may have tried to access a context high score in solo mode!") | ||
+ | end | ||
+ | <br/></source> |
Revision as of 10:45, 23 August 2018
Available since: Gideros 2018.3
Description
Fetch a specific leaderboard belonging to this Instant Game.
FBInstant.getLeaderboardAsync(name,callback)
Parameters
name: (string) The name of the leaderboard. Each leaderboard for an Instant Game must have its own distinct name.
callback: (function) A function that will be called with two arguments: Result with the matching leaderboard, rejecting if one is not found. An error code if the function failed.
Examples
Example
<br />
FBInstant.getLeaderboardAsync("my_aweome_leaderboard", function(result,error)
print("getLeaderboardAsync result:",result)
end)
<br/>
Example 2
<br />
FBInstant.getLeaderboardAsync("my_aweome_leaderboard", function(result,error)
print("name",result:getName()) -- should display 'my__awesome_leaderboard'
print("context id",result:getContextID()) -- eg 12345678 or null if not tied to a context
result:getEntryCountAsync( function (result,error) -- Fetches the total number of player entries in the leaderboard.
if result then
print("Count",result)
end
end)
--Update the player's score. If the player has an existing score, the old score will only be replaced if the new score is better than it.
-- NOTE: If the leaderboard is associated with a specific context, the game must be in that context to set a score for the player.
result:setScoreAsync(level,FBInstant.player.getName(),function(self,result,entry)
if result then
print("score ok",entry)
for key,val in pairs(entry.entry) do
print(key,val)
end
end
end)
-- Retrieves the leaderboard's entry for the current player, or null if the player has not set one yet.
result:getPlayerEntryAsync( function (self,result,entry)
if entry then
for key,val in pairs(entry.entry) do
print(key,val)
if key=="player" then
for key2,val2 in pairs(val) do
print(" ",key2,val2)
end
end
end
end
end)
-- Retrieve a set of leaderboard entries, ordered by score ranking in the leaderboard.
-- parameter: count number The number of entries to attempt to fetch from the leaderboard. Defaults to 10 if not specified. Up to a maximum of 100 entries may be fetched per query.
-- parameter: offset number The offset from the top of the leaderboard that entries will be fetched from.
result:getEntriesAsync(10,0,function(self,result,entries)
print(self,result,entries)
for loop=1,#entries do
local e=entries[loop].entry
print("at entry",loop)
for key,val in pairs(e) do
print(key,val)
if key=="player" then
for key2,val2 in pairs(val) do
print(" ",key2,val2)
end
end
end
end
end)
-- Most useful of all:
-- Retrieve a set of connected player leaderboard entries, ordered by score ranking in the leaderboard.
-- parameter: count number The number of entries to attempt to fetch from the leaderboard. Defaults to 10 if not specified. Up to a maximum of 100 entries may be fetched per query.
-- parameter: offset number The offset from the top of the leaderboard that entries will be fetched from.
result:getConnectedPlayerEntriesAsync(10,0,function(self,result,entries)
print(self,result,entries)
for loop=1,#entries do
local e=entries[loop].entry
print("at entry",loop)
for key,val in pairs(e) do
print(key,val)
if key=="player" then
for key2,val2 in pairs(val) do
print(" ",key2,val2)
end
end
end
end
end)
end)
<br/>
Example 3 - global leaderboard
<br />
FBInstant.getLeaderboardAsync("my_aweome_leaderboard",function (e,error)
print("global leaderboard event",e)
if e then
for key,value in pairs(e) do
print(key,value)
end
else
print("global leaderboard error",error)
end
end)
<br/>
Example 4 - group leaderboard
<br />
fbContextId=FBInstant.context.getID()
if fbContextId~="null" then
print("context",fbContextId)
FBInstant.getLeaderboardAsync("Highest Level Achievers."..fbContextId,function (e,error)
print("group leaderboard event",e)
print("group leaderboard error",error)
end)
else
print("You may have tried to access a context high score in solo mode!")
end
<br/>