Google Play load achievement description result

Hi,

In my app, when I unlock a Google Play achievement, content is unlocked in the app. The changes are saved after that.

When the user uninstalls the app, this save file is deleted, so I want a way for the user to get back the unlocked content he unlocked with the achievements.

I realized there’s a function that can check what achievements were unlocked, the loadAchievementDescriptions, but I don’t know how to use it, I searched for it but I couldn’t find what the function returns. I also don’t know how to make device logs.

I just want a way to know what achievements are unlocked. Does it return the achievements in order from the console?

Thank you again!

This page will outline the loadAchievements call.

Do a loadAchievements request call.  This will return (in your listener) all of your achievements, whether complete or not.  Then cycle through the event.data table, and look for isCompleted = true.

Here’s some code to get you started (not tested).

for k,v in pairs(event.data) do if v.isCompleted == true then -- completed achievement... end end

–john

Thanks, but how do I know which achievement is it? Do they come in order?

Definitely check out the page… the event.data table has a bunch of fields.  ID and description is part of it, so you can check the ID of the completed achievement and do what you have to do to give content back to the player.

–john

It’s still not working and I don’t know why.

My requestCallback only has this:

for i=1, 12 do if (event.data[i].isCompleted == true) then trophies[i] = true end end

and I’m not sure the code gets here.

The achievements may not be returning in an order you expect… you will need to check which achievement it is.  Note, I didn’t test this, but something like:

for i = 1, #event.data do if (event.data[i].isCompleted == true) then if event.data[i].identifier == "whateveryouridentiferis" then -- unlock elseif event.data[i].identifier == "anotheridentifier" then -- unlock this one end end end

It looks like the callBack function is not being acessed no matter what. Do you know what can be causing this?

I would need to see a little bit of code where you call init(), but it could be a typo, code, or a scope issue.

Thank you so much for taking your time helping me!

Ok, so I have a myData.lua that stores all information and loads and saves settings.

I call myData.init() in my main.lua, after making the Google Play login.

-- Init game network to use Google Play game services gameNetwork.init("google") -- Tries to automatically log in the user without displaying the login screen if the user doesn't want to login gameNetwork.request("login", { userInitiated = false }) myData.init() composer.gotoScene( "menu" )

At the end of myData.init() I call the M.loadAchievementListener(), this function:

function M.loadAchievementListener() gameNetwork.request("loadAchievementsDescription", {listener = M.loadAchievements}) end

The callBack function loadAchievements is this:

function M.loadAchievements(event) print("requestCallback") for i=1, 12 do if (event.data[i].isCompleted == true) then trophies[i] = true end end print("updatingTrophies") M.updateTrophies() loadsave.saveTable(trophies,"trophies.json") end

I even added some text to appear when this function is called but it does nothing.

Thanks once again

Hmm… hard to say looking at the snippets of code, but if M.loadAchievements is not outputting your print statements, then M.loadAchievementListener is not being called.

At this point, I would try to simplify to be sure everything is working and to narrow down the cause… place your init, login, and listener inside of main.lua. If that works, then there is some sort of issue with your myData library.

I tried putting all of it in the main, but the function is still not being acessed…

Looking a bit more closely, I this this…

gameNetwork.request("loadAchievementsDescription", {listener = M.loadAchievements})

should be this…

gameNetwork.request("loadAchievementDescriptions", {listener = M.loadAchievements})

Yeah, thanks, what a silly error. But I changed it and still not working…

Hi @jose.castanheira2,

Please try testing with a localized function located directly in the module, not added to another table. So something like this:

[lua]

local function loadAchievements( event )

   print( “loadAchievements function is being called!” )

end

gameNetwork.request( “loadAchievementDescriptions”, { listener=loadAchievements } )

[/lua]

Then report back if this is working.

Brent

It worked on the main, but not on myData. Good enough, I guess!

This page will outline the loadAchievements call.

Do a loadAchievements request call.  This will return (in your listener) all of your achievements, whether complete or not.  Then cycle through the event.data table, and look for isCompleted = true.

Here’s some code to get you started (not tested).

for k,v in pairs(event.data) do if v.isCompleted == true then -- completed achievement... end end

–john

Thanks, but how do I know which achievement is it? Do they come in order?

Definitely check out the page… the event.data table has a bunch of fields.  ID and description is part of it, so you can check the ID of the completed achievement and do what you have to do to give content back to the player.

–john

It’s still not working and I don’t know why.

My requestCallback only has this:

for i=1, 12 do if (event.data[i].isCompleted == true) then trophies[i] = true end end

and I’m not sure the code gets here.

The achievements may not be returning in an order you expect… you will need to check which achievement it is.  Note, I didn’t test this, but something like:

for i = 1, #event.data do if (event.data[i].isCompleted == true) then if event.data[i].identifier == "whateveryouridentiferis" then -- unlock elseif event.data[i].identifier == "anotheridentifier" then -- unlock this one end end end