0

This is my code:

//Code for host
case (GameState.PlayingAsHost):
{
  session.Update();

  while (session.LocalGamers[0].IsDataAvailable) //Recieve data
  {
     NetworkGamer sender;
     PacketReader reader = new PacketReader();

     session.LocalGamers[0].ReceiveData(reader, out sender);

     guestPaddle.Position = reader.ReadVector2();
  }

  hostPaddle.Update(); //Update paddle position

  var packetWriter = new PacketWriter(); //Send data
  packetWriter.Write(new Vector2(hostPaddle.Position.X, 50));
  session.LocalGamers[0].SendData(packetWriter,SendDataOptions.InOrder);

  break;
}

//Guest Code
case (GameState.PlayingAsGuest):
{
  session.Update();

  while (session.LocalGamers[0].IsDataAvailable)//Recieve data
  {
     NetworkGamer sender;
     PacketReader reader = new PacketReader();

     session.LocalGamers[0].ReceiveData(reader, out sender);

     hostPaddle.Position = reader.ReadVector2();
  }

  guestPaddle.Update(); //Update paddle position

  var packetWriter = new PacketWriter(); //Send data
  packetWriter.Write(new Vector2(guestPaddle.Position.X, 50));
  session.LocalGamers[0].SendData(packetWriter, SendDataOptions.InOrder);

  break;
}

This is going to be a pong multiplayer(therefore the paddle names). Before when the guest sended it's package before recieving,the code worked except for that when playing as guest the host's X position was set to the guest's X position. I was testing if changing so that the guest and host recieved data before sending it would solve the problem. By doing so the host gets the error message:"Unable to read beyond the end of the stream". How do I solve this problem?

1 Answers1

0

I assume your switch and case are just because it could be a more efficient way than if's.

In order to reproduce the behavior of if's, you need to either put a break in your cases or contain it in brackets. I'm not sure if you realized this, or if it is the problem, but your brackets seem to certainly be messed up, because your guest case is inside your host case.

//Code for host
case (GameState.PlayingAsHost):
{
  session.Update();

  while (session.LocalGamers[0].IsDataAvailable) //Recieve data
  {
     NetworkGamer sender;
     PacketReader reader = new PacketReader();

     session.LocalGamers[0].ReceiveData(reader, out sender);

     guestPaddle.Position = reader.ReadVector2();
  }

  hostPaddle.Update();

  var packetWriter = new PacketWriter(); //Send data
  packetWriter.Write(new Vector2(hostPaddle.Position.X, 50));
  session.LocalGamers[0].SendData(packetWriter,SendDataOptions.InOrder);
}

//Guest Code
case (GameState.PlayingAsGuest):
{
  session.Update();

  while (session.LocalGamers[0].IsDataAvailable)//Recieve data
  {
     NetworkGamer sender;
     PacketReader reader = new PacketReader();

     session.LocalGamers[0].ReceiveData(reader, out sender);

     hostPaddle.Position = reader.ReadVector2();
  }

  guestPaddle.Update();

  var packetWriter = new PacketWriter(); //Send data
  packetWriter.Write(new Vector2(guestPaddle.Position.X, 50));
  session.LocalGamers[0].SendData(packetWriter, SendDataOptions.InOrder);
}

This should work, I'm not sure what is in the methods so you may have to move *paddle.Update() out of the cases.

Community
  • 1
  • 1
Cyral
  • 13,999
  • 6
  • 50
  • 90
  • I'm sorry if my information is lacking, I do have breaks at the end of each case and the Update() methods are just for moving the paddle to the mouse position. I do have more cases than these 2 it's just that they didn't serve a purpose in this question. The answer I am seeking is why the: while(session.LocalGamers[0].IsDataAvailable) method would run if the host Is not able to read the package. – Eric Nilsson Oct 24 '13 at 07:08