Forum Multiplayer Message.length Strange Behavior

Discussion and help relating to the PlayerIO Multiplayer API.

Message.length Strange Behavior

Postby AK712 » November 12th, 2016, 6:45 pm

Hello everyone,

I've been hitting a wall on this issue and I can't seem to resolve it. It's very strange and I haven't had it before.

In my C# code, I have the "init" function return the user's ID and their username for each user. I then send this information to the AS3 "init", and of course since we don't know how long the message will be (since it depends on the number of users), I use a for loop. Yet this loop is never executed; indeed, any time I try to mention m.length anywhere in my AS3 "init" code, the game breaks. Here's my code:

C#:
Code: Select all
public override void UserJoined(Player player)
{
   Message newInit = Message.Create("init");

   if (totalPlayers == 0)
   {
      owner = player;
      playersIn[0] = player;
      totalPlayers++;
   }
   else
   {
      for (int i = 0; i < 50; i++)
      {
         if (playersIn[i] == null)
         {
            playersIn[i] = player;
            totalPlayers++;
            i = 50;
         }
      }
   }
   newInit.Add(player.Id, player.username, owner.Id, owner.username);
   for (int j = 0; j < 50; j++)
   {
      if (playersIn[j] != null && playersIn[j] != player)
      newInit.Add(j,playersIn[j].Id, playersIn[j].username);
   }
   player.Send(newInit);
}


This works fine, and always outputs a message with a finite length.

AS3:
Code: Select all
connection.addMessageHandler("init", function(m:Message, thisID:int, thisName:String, theOwner:int, ownerUser:String)
{
   trace(m.length) //breaks
   testbox.text = m.length.toString() //breaks
   totalPlayers++
   myID = thisID
   myName = thisName
   ownerID = theOwner
   ownerName = ownerUser
   for (var a = 4; a < m.length; a+=3)//Never executes
   {
      userIDs[m.getInt(a)] = m.getInt(a + 1)
      userNames[m.getInt(a)] = m.getString(a + 2)
   }
})


I have no clue how to move forward on this. Can anyone help?
User avatar
AK712
 
Posts: 34
Joined: May 2nd, 2012, 12:43 am

Re: Message.length Strange Behavior

Postby robscherer123 » November 14th, 2016, 4:56 pm

When you says it "breaks", what do you mean exactly? What is the error message and # are you receiving in AS3?
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm

Re: Message.length Strange Behavior

Postby AK712 » November 15th, 2016, 4:35 am

That's the problem. It doesn't even trace. Shifting the trace and testbox.text lower makes everything above it execute fully, but the moment the code reaches any mention of m.length, the code basically stops functioning altogether.
User avatar
AK712
 
Posts: 34
Joined: May 2nd, 2012, 12:43 am

Re: Message.length Strange Behavior

Postby robscherer123 » November 15th, 2016, 5:08 am

Well that's incredibly weird then. The trace isn't reporting anything, not even blank space? It sounds as if an error is happening, but it's not being reported.

Ya know what!... I believe internally Playerio nests your callback function in a try/catch and then reports the error to your error log inside the catch statement. I'm still not sure why the message length would be throwing an error though. What if you try un-nesting your function like you show in the example. Perhaps that will fix it.
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm

Re: Message.length Strange Behavior

Postby Henrik » November 18th, 2016, 3:08 am

Hey AK712,

What happens if you run that code in a debug Flash player?

I did some tests around adding null values to a Message, and in a debug player it shows the exception being thrown, but in a regular player it appears to just stop executing.

Now you shouldn't be able to add null values to Messages anywhere, but there might be a bug somewhere that allows you to in some cases, and I could see how that would result in the C# server code serializing it into something that the AS3 client will only partially deserialize, resulting in a half-broken Message that errors out when you try to check its length. That's my best guess.

So try running your code in a debug Flash player, that should give you more information.

And do a check of the Message before you send it on the serverside to see if any of the values are null?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Message.length Strange Behavior

Postby Emalton » April 23rd, 2018, 10:12 pm

You should use ForEachPlayer on the server sided code instead of that loop.
Emalton
 
Posts: 86
Joined: June 28th, 2013, 3:49 am


Return to Multiplayer



cron