Corona Cloud Guides > Get Leaderboard Scores with errors

In this page:http://docs.coronalabs.com/guide/cloud/leaderachieve/index.html#leaderboard-methods

there is this code:

local function leaderboardsListener( event )

if ( event.type == “getScores” and event.error == nil ) then

local scoreStrings = {}
local results = event.results
local maxScores = #results

if ( maxScores > 10 ) then maxScores = 10 end --get maximum of 10 high scores

for i = 1, maxScores do
local text = string.format( “%-2d - %-15s - %6d”, i, string.sub(results[i].username, 1, 15), results[i].value )
scoreStrings[i] = display.newText( text, 0, 0, nil, 16 )
scoreStrings[i].x = display.contentCenterX - 32
scoreStrings[i].y = 100 + i * 24
end
end
end
end

where I found some problems:

First of all one “end” should be removed because there are 5 ends (one is in the same line of the if (maxScore > 10)

This is not so difficult to find as error, but I’m just wondering if this code was ever tested. Before to copy and paste a code I would test it.

Second problem:

local results = event.results

is wrong, should be 

local results = event.response

based on other info found in another piece of code…

 event.results is giving me an error

Third problem is that the test 

if ( event.type == “getScores” and event.error == nil )

where even.error == nil  is not working for me.

Adding the line:

print(“in leaderboardsListener event.name:”,event.name," event.type:",event.type," event.error:",event.error," event.response:",event.response )

I have this output:

in leaderboardsListener event.name:    leaderboards     event.type:    getScores     event.error:    nil     event.response:    {“error”:“User authentication token is not present in the request parameters.”}

So the event.error is nill but still I have an error that is indicated in the even.response

Now obviously I’m doing something wrong… but I do not understand what.

Also, when I try to see the scores in the leaderboard in the cloud dashboard I do see only a white page (i’m clicking on the link Score in this leaderboard).

I think (but I’m not sure) I’ve submitted a score, but I’m not able to see this neither via code, nor via the dashboard…

Thank you,

Ema

I have been trying to do this today too, and came across the same problems. You have to register a user and login before you call leaderboard.getScores() thats why you are getting  {“error”:“User authentication token is not present in the request parameters.”}

I am using the same leaderboardListener you have, which is showing a list of scores from 3 different user accounts I have created in the simulator, but I am still getting the blank white page when I try to see my leaderboard in the Corona Cloud Dashboard

Also I read in another post that that the event.response is returned as a json so where you have

local results = event.response

I changed it to

local results = json.decode( event.response )

Thank you Gooner87,

I just realized that I need to call this after the user is logged.

I was also able to submit a score to a leaderboard (I saw the response correctly).

But now when I try to call leaderboards.getScores( myLeaderBoard )  (I’m doing this just after the login):

with this:

function leaderboardsListener( event )

    print(“in leaderboardsListener event.name:”,event.name," event.type:",event.type," event.error:",event.error," event.response:",event.response )

    if ( event.type == “getScores” and event.error == nil ) then

        local results = event.response – not event.results

        print("#results: ",#results)

            for i = 1, #results do

                print(“i=”,i)

                print(“user:”, results[i]," score:",results[i])

            end

        end

end

THe result is that I have 

#results:     259    

 user:    nil     score:    nil    

 i=    2    

 user:    nil     score:    nil    

 i=    3    

 user:    nil     score:    nil    

continues until…

 user:    nil     score:    nil    

 i=    259    

 user:    nil     score:    nil

Ema

Ema,

Just looking at your listener, you are trying to print(“user:”, results[i]," score:",results[i]) should this not be

print(“user:”, results[i].username," score:",results[i].value)

this is my listener

[lua]

local function leaderboardsListener( event )

    if ( event.type == “getScores” and event.error == nil ) then

        print(“in leaderboardsListener event.name:”,event.name," event.type:",event.type," event.error:",event.error," event.response:",event.response )

        local scoreStrings = {}

        local results = json.decode( event.response )

        local maxScores = #results

        if ( maxScores > 10 ) then maxScores = 10 end  --get maximum of 10 high scores

            for i = 1, maxScores do

                local text = string.format( “%-2d - %-15s - %6d”, i, string.sub(results[i].username, 1, 15), results[i].value )

                scoreStrings[i] = display.newText( text, 0, 0, nil, 16 )

                scoreStrings[i].x = display.contentCenterX - 32

                scoreStrings[i].y = 100 + i * 24

            end

        end

    end

[/lua]

The problem is that if results[i] is nil, results[i].username will throw an exception.

What I do not get is that #results is a number, but the actual table seems to be empty.
Also Lua Glider watch shows [].

Is it your listner working?

Ema
 

Your code is working… Just tried…

Interesting.

Many thanks.

I’ll try to implement a UI for the leaderboard soon. I’ll share my code if I get it.

Ema

Ha was just writing to tell you that I swapped in your listener in my code and was getting the same result as you.

You are welcome. I was just wondering, have you created your own UI for registering users and logging in or are you using the gameNetwork dashboard?

Let me look into it.  Something may have gotten missed in the change to the API.

will the api change this? response thing?

Okay I worked on the leaderboard code and found out where some stuff got missed when copying/pasting and the new API.  Here is the working code:

  local leaderboards = cloud.leaderboards ------- local function leaderboardsListener( event )     if ( event.type == "getScores" and event.error == nil ) then         local scoreStrings = {}         local results = json.decode(event.response)         local maxScores = #results         if ( maxScores \> 10 ) then maxScores = 10 end  --get maximum of 10 high scores         for i = 1, maxScores do             local text = string.format( "%-2d - %-15s - %6d", i, string.sub(results[i].username, 1, 15), results[i].value )             scoreStrings[i] = display.newText( text, 0, 0, "Courier", 16 )             scoreStrings[i]:setReferencePoint(display.TopLeftReferencePoint)             scoreStrings[i].x = 20             scoreStrings[i].y = 100 + i \* 24         end     elseif event.type == "submitHighScore" then         print(event.response)     end end leaderboards.setListener( leaderboardsListener )  

The old API did the json.decode for you and returned event.results, the new API returns event.response and doesn’t json.decode the data for the calls.

We are getting the guide updated.

I have been trying to do this today too, and came across the same problems. You have to register a user and login before you call leaderboard.getScores() thats why you are getting  {“error”:“User authentication token is not present in the request parameters.”}

I am using the same leaderboardListener you have, which is showing a list of scores from 3 different user accounts I have created in the simulator, but I am still getting the blank white page when I try to see my leaderboard in the Corona Cloud Dashboard

Also I read in another post that that the event.response is returned as a json so where you have

local results = event.response

I changed it to

local results = json.decode( event.response )

Thank you Gooner87,

I just realized that I need to call this after the user is logged.

I was also able to submit a score to a leaderboard (I saw the response correctly).

But now when I try to call leaderboards.getScores( myLeaderBoard )  (I’m doing this just after the login):

with this:

function leaderboardsListener( event )

    print(“in leaderboardsListener event.name:”,event.name," event.type:",event.type," event.error:",event.error," event.response:",event.response )

    if ( event.type == “getScores” and event.error == nil ) then

        local results = event.response – not event.results

        print("#results: ",#results)

            for i = 1, #results do

                print(“i=”,i)

                print(“user:”, results[i]," score:",results[i])

            end

        end

end

THe result is that I have 

#results:     259    

 user:    nil     score:    nil    

 i=    2    

 user:    nil     score:    nil    

 i=    3    

 user:    nil     score:    nil    

continues until…

 user:    nil     score:    nil    

 i=    259    

 user:    nil     score:    nil

Ema

Ema,

Just looking at your listener, you are trying to print(“user:”, results[i]," score:",results[i]) should this not be

print(“user:”, results[i].username," score:",results[i].value)

this is my listener

[lua]

local function leaderboardsListener( event )

    if ( event.type == “getScores” and event.error == nil ) then

        print(“in leaderboardsListener event.name:”,event.name," event.type:",event.type," event.error:",event.error," event.response:",event.response )

        local scoreStrings = {}

        local results = json.decode( event.response )

        local maxScores = #results

        if ( maxScores > 10 ) then maxScores = 10 end  --get maximum of 10 high scores

            for i = 1, maxScores do

                local text = string.format( “%-2d - %-15s - %6d”, i, string.sub(results[i].username, 1, 15), results[i].value )

                scoreStrings[i] = display.newText( text, 0, 0, nil, 16 )

                scoreStrings[i].x = display.contentCenterX - 32

                scoreStrings[i].y = 100 + i * 24

            end

        end

    end

[/lua]

The problem is that if results[i] is nil, results[i].username will throw an exception.

What I do not get is that #results is a number, but the actual table seems to be empty.
Also Lua Glider watch shows [].

Is it your listner working?

Ema
 

Your code is working… Just tried…

Interesting.

Many thanks.

I’ll try to implement a UI for the leaderboard soon. I’ll share my code if I get it.

Ema

Ha was just writing to tell you that I swapped in your listener in my code and was getting the same result as you.

You are welcome. I was just wondering, have you created your own UI for registering users and logging in or are you using the gameNetwork dashboard?

Let me look into it.  Something may have gotten missed in the change to the API.

will the api change this? response thing?

Okay I worked on the leaderboard code and found out where some stuff got missed when copying/pasting and the new API.  Here is the working code:

  local leaderboards = cloud.leaderboards ------- local function leaderboardsListener( event )     if ( event.type == "getScores" and event.error == nil ) then         local scoreStrings = {}         local results = json.decode(event.response)         local maxScores = #results         if ( maxScores \> 10 ) then maxScores = 10 end  --get maximum of 10 high scores         for i = 1, maxScores do             local text = string.format( "%-2d - %-15s - %6d", i, string.sub(results[i].username, 1, 15), results[i].value )             scoreStrings[i] = display.newText( text, 0, 0, "Courier", 16 )             scoreStrings[i]:setReferencePoint(display.TopLeftReferencePoint)             scoreStrings[i].x = 20             scoreStrings[i].y = 100 + i \* 24         end     elseif event.type == "submitHighScore" then         print(event.response)     end end leaderboards.setListener( leaderboardsListener )  

The old API did the json.decode for you and returned event.results, the new API returns event.response and doesn’t json.decode the data for the calls.

We are getting the guide updated.