Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Code stops running after MessageBase.Deserialize

Discussion in 'Multiplayer' started by PaulRdy, Jul 21, 2017.

  1. PaulRdy

    PaulRdy

    Joined:
    Jun 26, 2015
    Posts:
    17
    Hey guys,
    I have an issue that my code seemingly just stops running after MessageBase.Deserialize is called.

    Code (CSharp):
    1.  
    2. public override void Deserialize(NetworkReader reader)
    3.         {
    4. data = new Player();
    5.             data.ID = reader.ReadInt32();
    6.             data.HP = reader.ReadInt32();
    7.             data.state = (PlayerStates)reader.ReadInt32();
    8.             data.fieldSideIndex = reader.ReadInt32();
    9.             data.hand = new List<Card>();
    10.             for (int i = 0; i < GameConfig.maxHandSize; i++)
    11.             {
    12.                 if (!reader.ReadBoolean())
    13.                 {
    14.                     continue;
    15.                 }
    16.                 data.hand.Add(Card.Deserialize(reader));
    17.             }
    18.             data.deck = new List<Card>();
    19.             for (int i = 0; i < GameConfig.deckSize; i++)
    20.             {
    21.              
    22.                 if (!reader.ReadBoolean())
    23.                 {
    24.                     continue;
    25.                 }
    26.              
    27.                 data.deck.Add(Card.Deserialize(reader));
    28.             }
    29.             UnityEngine.Debug.Log("Finished deserializing player update message with deck...");
    30. }
    The last Debug.Log here gets called; I have checked via VisualStudio attaching all data in the class has been properly deserialized.

    Code (CSharp):
    1.  public void CardDrawn (Player p)
    2.     {
    3.         NetworkServer.SendToAll(GameConfig.PlayerDrawNetworkMessage, new PlayerUpdateMessageWithDeck(p));
    4.     }
    5.     private void OnDrawRecieved (NetworkMessage netMsg)
    6.     {
    7.         PlayerUpdateMessageWithDeck updatedPlayer = netMsg.ReadMessage<PlayerUpdateMessageWithDeck>();
    8.         Debug.Log("draw recieved");
    9.         if (updatedPlayer.data.ID == instance.ID)
    10.         {
    11.             Debug.Log("local player draw");
    12.             if (instance.LocalPlayerDraw != null)
    13.                 instance.LocalPlayerDraw(updatedPlayer.data);
    14.         }
    15.         else
    16.         {
    17.             Debug.Log("remote player draw");
    18.             if (instance.RemotePlayerDraw != null)
    19.                 instance.RemotePlayerDraw(updatedPlayer.data);
    20.         }
    21.     }
    The "CardDrawn"-Method gets called aswell. However, Debug.Log("Draw recieved") does not get called anymore. I'm sort of lost right now. Especially since this:

    Code (CSharp):
    1. public void OnFieldUpdateRecieved (NetworkMessage m)
    2.     {
    3.         PlayingFieldMessage updatedField = m.ReadMessage<PlayingFieldMessage>();
    4.         Debug.Log(updatedField.field.ToString());
    5.     }
    works perfectly fine and uses the same way of serializing / deserializing and sending the message.

    Hope anyone has any ideas...
    With best regards
    - Paul
     
  2. PaulRdy

    PaulRdy

    Joined:
    Jun 26, 2015
    Posts:
    17
    Update: Okay guys. Found the solution. Since you specify the message type on serialization all handlers for that message type get invoked when the message is recieved. You cannot simply declare another message type, register a handler for that message type and then use the same message class to send the message.

    The methods which are registered for the message type specified in "OnSerialize" will get invoked.