Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Error in my MessageBase inheritance tree

Discussion in 'Multiplayer' started by ADaurio, Nov 24, 2017.

  1. ADaurio

    ADaurio

    Joined:
    Jul 12, 2015
    Posts:
    21
    Hi everybody. I have several types of custom MessageBases. One of them acts as a base class for most of the others. As an example:

    MessageBase > LockstepMessage > SelectMessage

    My LockstepMessage class looks like this:

    Code (csharp):
    1.  
    2. [Serializable]
    3. public class LockstepMessage : MessageBase
    4. {
    5.     public int team;
    6.     public int frame;
    7.     public float averageGameFrames;
    8.     public float averagePacketTime;
    9.  
    10.     public LockstepMessage()
    11.     {
    12.         team = NetworkInfo.localPlayer;
    13.         frame = NetworkInfo.commandNumber;
    14.         NetworkInfo.commandNumber++;
    15.         averageGameFrames = FrameManager.instance.GetAverageFrameRate();
    16.     }
    17. }
    18.  
    When I use a Network Writer/Reader to send/receive a derived message:

    Code (csharp):
    1.  
    2.         NetworkWriter nw = new NetworkWriter();
    3.         nw.StartMessage(MessageTypes.SelectToServer);
    4.         select.Serialize(nw);
    5.         nw.FinishMessage();
    6.  
    Code (csharp):
    1.  
    2.         SelectMessage select = netMsg.ReadMessage<SelectMessage>();
    3.  
    The base class variables don't seem to deserialize properly, but the derived class (SelectMessage) members deserialize fine. Is it because my SES sets the vars? What's the proper way to do this?

    Thanks for your time.
     
    Last edited: Nov 24, 2017
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
  3. ADaurio

    ADaurio

    Joined:
    Jul 12, 2015
    Posts:
    21
    Thanks, that helped a lot. Manually overriding the Serialize and Deserialize functions worked perfectly. Unfortunately, this brings up a new problem. How do you deserialize a List of ints with a NetworkReader?
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    To serialize and deserialize lists you are going to need to maybe make an extension method or something that you can pass the list to serialize.
    The serialize will need to first serialize the Count of the list and then it will loop over and serialize the list itself.
    The deserialize will need to then read the bytes to get the Count first, and then it can loop using the Count and deserialize all the contents.