Frustration May Be Blinding Me To What I'm Doing Wrong With This

I’m trying to use data collected from getMatches() in a table and I just don’t get what I’m doing wrong.  I’m pretty frustrated overall with cloud, since I haven’t gotten an answer to many questions I have, but I’m plugging away to try and get what I can to work.

[Lua]

lobbyUI = function ()

                print(“lobby called”)

                local matchTable = {} – Table I want to hold the results from getMatches

                

                --EVENT LISTENER

                function ccMatches( event )

                    if ( event.type == “Matches” ) then 

                        print( “Get Matches successful” )

                        --event.results = table of values related to scores

                        local results = event.results

                        local maxMatches = #results

                        if ( maxMatches > 30 ) then maxMatches = 30 end  --get maximum of 30 matches

                        for i = 1, maxMatches do

                            local matchID = results[i]._id 

                            print(matchID)  – Prints correct answer  

                           

                        end

                        

                        matchTable = results        – Already created matchTable above…

                        print(matchTable[1]._id)   – correct

                        print(#matchTable)           – correct

                        

                    elseif ( event.type == “MatchCreated” ) then

                        print( “Create Match Successful” )

                        local results = event.results

                        

                    end   

                end

                Runtime:addEventListener( “Multiplayer”, ccMatches )

                

                coronaCloud.getMatches()

                

                – Got match results, which means that results should have been assigned to my table matchTable {}

                print(“Testing”)

                print(matchTable[1]._id) – Nothing

                print(#matchTable)         – error attempt to index field  “a nil value”

[/Lua]

I don’t understand why matchTable is empty.  It’s got to be a scope issue of some kind, but I don’t get why.

I’m probably doing something wrong that is obvious, let me know

I’m nearly ready to give up on cloud altogether at this point.  I’m sure it will be worthwhile for some things once they are ready to support it, but I have not been able to get an answer for my primary question as to how to handle my game json files that are loaded and saved by players during their turns.

I’ve been able to make login/reg work and look good, I can load news, create/delete matches, create/delete chats, but that’s as far as I’ve been able to take it.  All of which is meaningless if cloud won’t actually serve my multiplayer needs.

I went through the needed steps to get push notifications set up.  I get told that device is registered in the simulator output, but when I try and send a push from the cloud console, it tells me there are no devices registered.

So, all together a bunch of issues that I can’t solve making me very frustrated.  I just want to add multiplayer to my app… is that to much to ask for.

If can’t afford to waste much more time on cloud, which probably means I have to spend a bunch of time learning how to use pubnub and fork out what seems like a lot of money… I wish I had the knowledge needed to make noobhub work for me.

        

Hi there,

The reason why the print statements in lines 40 and 41 of your code aren’t printing what you expect them to is that getMatches() works asynchronously.  This means that your code won’t “wait” at line 36 for a callback before proceeding to lines 40 and 41.  Line 36 merely *launches* the getMatches request, but it’ll take a few tens of milliseconds to several seconds (depending on your network connection) to actually get the results, which are then processed in your ccMatch callback function.  Meanwhile, while the request is in flight, your code will continue to proceed to lines 40 and 41, and at that time matchTable will still be empty (since the request hasn’t completed yet, it only just launched).

Said differently, the comment you wrote in line 38 isn’t actually correct.  At that point, it’s not true that you’ve gotten the matches, only that you’ve launched a request for one.

Hope this helps!

  • Andrew

That makes sense.

I’ll see if I can put dummy info in to start and fire an update function after the getMatches has actually completed.

that did the trick, thanks Andrew

Does anyone know how to set fields like name, when creating a new match.

I can now display data from getMatches, but there isn’t anything “friendly” about that data.

When a match is created, I also create a chat.  You can set the name of chats, but the same process doesn’t seem to set the name of a match.

My solution to this problem was to make every first move of a match contain information about that match. So,

  1. createMatch
  2. startMatch
  3. submit first move that contains name, rules etc.
  4. now the game can really begin

You can get the moves content by calling getRecentMoves the 1st move that has been made will always be the last in the table that getRecentMoves gives you back.

With this approach you are able to add custom information to your match.

Hope this helps!

Thanks for the tips qwertier.

I’ve been trying to think of ways to trick around the apparent whole.

The problem is I have empire data, leader data, map data etc, and it’s a lot.  I need to exchange the data every turn, because resource levels change, leader data changes, even the map changes every turn.

I can see how that would be great for passing some info into the game though.

What do you mean by “set fields like name” when creating a new match?  Player names?  What kind of game data do you need to pass?

Back in my first attempt with Game Minion (before it was Corona Cloud), I tried to make data packets that had a bunch of pipe separated values and was super frustrated until I realized that JSON was my answer.  I switched that game and in the ones I’ve worked since including Corona Cloud, I simply have a Lua table with the values I need to pass around for each move.  I do a json.encode(gameData) and take the string returned from that and make it my submitMove()'s content parameter.  I’m sure there are some practical limits to the size of the table. 

Hi Rob,

by set a field like “name”, I meant that when you create a chat, you pass a name for the instance of that chat.  Wondered if I could do that with create match, because match does have a name field in its table.

I have 5 different json files holding the five game tables.  Sometimes, on double layer maps, I have 6 json files.  They total up to around 2.5k.

I didn’t think it would be possible for me to put them into a single json file… can you have multiple tables in 1 json file?

I’ve never tried putting multiple tables in one JSON file, but in theory it should work.  If not, just JSON encode each one and cat them all together in one big string using some special character like a | to separate them.

I was thinking about that possibility yesterday some, because I also want to pass some info into the specific chatrooms, like max players, etc.

I don’t know the mechanics for breaking down the combined string.  Roughly, how would you set up the process to extract the bits you want from the string assuming your using | as the dividing character?

I include this little function in my extras module that I load.  It adds a “split” function to the string library:

[lua]

  function string:split( inSplitPattern, outResults )
      if not outResults then
          outResults = { }
      end
      local theStart = 1
      local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
      while theSplitStart do
          table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
          theStart = theSplitEnd + 1
          theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
      end
      table.insert( outResults, string.sub( self, theStart ) )
      return outResults
  end

[/lua]

then you can do:

     someTableOfSubstrings = mylongpipestring:split("|")

or something like that.

Hi there,

The reason why the print statements in lines 40 and 41 of your code aren’t printing what you expect them to is that getMatches() works asynchronously.  This means that your code won’t “wait” at line 36 for a callback before proceeding to lines 40 and 41.  Line 36 merely *launches* the getMatches request, but it’ll take a few tens of milliseconds to several seconds (depending on your network connection) to actually get the results, which are then processed in your ccMatch callback function.  Meanwhile, while the request is in flight, your code will continue to proceed to lines 40 and 41, and at that time matchTable will still be empty (since the request hasn’t completed yet, it only just launched).

Said differently, the comment you wrote in line 38 isn’t actually correct.  At that point, it’s not true that you’ve gotten the matches, only that you’ve launched a request for one.

Hope this helps!

  • Andrew

That makes sense.

I’ll see if I can put dummy info in to start and fire an update function after the getMatches has actually completed.

that did the trick, thanks Andrew

Does anyone know how to set fields like name, when creating a new match.

I can now display data from getMatches, but there isn’t anything “friendly” about that data.

When a match is created, I also create a chat.  You can set the name of chats, but the same process doesn’t seem to set the name of a match.

My solution to this problem was to make every first move of a match contain information about that match. So,

  1. createMatch
  2. startMatch
  3. submit first move that contains name, rules etc.
  4. now the game can really begin

You can get the moves content by calling getRecentMoves the 1st move that has been made will always be the last in the table that getRecentMoves gives you back.

With this approach you are able to add custom information to your match.

Hope this helps!

Thanks for the tips qwertier.

I’ve been trying to think of ways to trick around the apparent whole.

The problem is I have empire data, leader data, map data etc, and it’s a lot.  I need to exchange the data every turn, because resource levels change, leader data changes, even the map changes every turn.

I can see how that would be great for passing some info into the game though.

What do you mean by “set fields like name” when creating a new match?  Player names?  What kind of game data do you need to pass?

Back in my first attempt with Game Minion (before it was Corona Cloud), I tried to make data packets that had a bunch of pipe separated values and was super frustrated until I realized that JSON was my answer.  I switched that game and in the ones I’ve worked since including Corona Cloud, I simply have a Lua table with the values I need to pass around for each move.  I do a json.encode(gameData) and take the string returned from that and make it my submitMove()'s content parameter.  I’m sure there are some practical limits to the size of the table. 

Hi Rob,

by set a field like “name”, I meant that when you create a chat, you pass a name for the instance of that chat.  Wondered if I could do that with create match, because match does have a name field in its table.

I have 5 different json files holding the five game tables.  Sometimes, on double layer maps, I have 6 json files.  They total up to around 2.5k.

I didn’t think it would be possible for me to put them into a single json file… can you have multiple tables in 1 json file?

I’ve never tried putting multiple tables in one JSON file, but in theory it should work.  If not, just JSON encode each one and cat them all together in one big string using some special character like a | to separate them.

I was thinking about that possibility yesterday some, because I also want to pass some info into the specific chatrooms, like max players, etc.

I don’t know the mechanics for breaking down the combined string.  Roughly, how would you set up the process to extract the bits you want from the string assuming your using | as the dividing character?