Hello, I've encounter a problem that's had me stumped for at least a few days now. It has to do with the Gideros REST api for Game Minion, but my issue may be as a result of general Lua coding issues. I'll try to keep this short.
Here is a condensed version of my code:
-- Initialize GameMinion object
gm = GameMinion.new(accessKey, secretKey)
gm.data = {}
authToken = nil --initialize global authtoken
leaderBoard = "leaderboard Code" -- test board
online = true -- global variable that determines where leaderboard Data is grabbed from
loginDialog() -- login function that takes user through login process
--***Here's where the trouble starts****
-- the following is in a leaderboard scene within the game:
if self.online == false then
-- a simple for loop. This works fine. Grabs data for local leaderboard
elseif self.online == true then
print("User logged in. About to retrieve data") --this prints
gm:getHighScores(leaderBoard) --returns nothing!
end |
When I run my program using the above code, gm:getHighScores(leaderBoard) does not return anything, and I cannot figure out why! What is particularly strange to me is that if I put the function elsewhere, like in loginUser(), it works:
--accepts table with username and password
function loginUser(t)
--
local function onComplete(event)
print(event.buttonIndex, event.buttonText)
end
--login
gm:login(t[1], t[2], {
success = function(r)
authToken = gm.authToken --> works
print("AuthToken is: "..authToken)
if authToken then
local alertDialog = AlertDialog.new("Login", "Looks Like It Worked!", "Neato!")
alertDialog:addEventListener(Event.COMPLETE, onComplete)
alertDialog:show()
--****works fine if I put it here******
gm:getHighScores(leaderBoard) -- returns normal string as it should
else
local alertDialog = AlertDialog.new("Login", "Something Fucked Up.", "Well... Shit")
alertDialog:addEventListener(Event.COMPLETE, onComplete)
alertDialog:show()
end
end,
error = function(r) print("Error") end,})
end |
Alright, so there's that mystery, but I also have one last thing that has me confused about the REST api. How do I return data to variables within my code? Here's an example using gm:getHighScores():
table = {}
gm:getHighScores("leaderboard ID", function(r)
table = r.data -- take returned data table and save to "table"
end)
print(table[1].username) -- fails |
Can I not use tables and/or the data from function(r) like this? What do I not understand here?
Comments
In your first code I don't see where you call login function
but about saving data response from REST API, I think that word table is a reserved word for table class and you cant use it as variable, you can pick any other name like:
A theoretical solution to this would be to have some sort of function check the status or responseData and call the function that uses said data only after it is no longer nil.
...I tried a while loop, but that kind of froze myt game.....
yep, you're right. That works like callback after connection was established and data retrieved.
So your solution might be to put all other related call in another function, and call this function inside this callback