Search Unity

Third Party PUN Serialization

Discussion in 'Multiplayer' started by KMKxJOEY1, May 17, 2016.

  1. KMKxJOEY1

    KMKxJOEY1

    Joined:
    Jan 2, 2013
    Posts:
    10
    Hello,

    I have been working on a dynamic day cycle system for my game and am now struggling to get it to work on other clients. The error is Exception: cannot serialize(): DynamicDayCycle+DynamicData.

    I am under the impression that PUN can only serialize basic data types such as numbers, bools, Vectors, etc. Well I have a couple class 'packages' that only contains these basic data types. Take a look:

    Code (CSharp):
    1.  
    2.     /// <summary>
    3.     /// Dynamic data to serialize consisting of sun and weather.
    4.     /// </summary>
    5.     [Serializable]
    6.     public class DynamicData
    7.     {
    8.         /// <summary>
    9.         /// The sun data.
    10.         /// </summary>
    11.         public SunData sunData = new SunData();
    12.         /// <summary>
    13.         /// The current weather.
    14.         /// </summary>
    15.         public Weather currentWeather;
    16.         /// <summary>
    17.         /// The current time.
    18.         /// </summary>
    19.         public float currentTime;
    20.  
    21.         /// <summary>
    22.         /// Updates the DynamicData package to serialize.
    23.         /// </summary>
    24.         /// <param name="position">The current position of the sun.</param>
    25.         /// <param name="rotation">The current rotation of the sun.</param>
    26.         /// <param name="color">The current color of the sun.</param>
    27.         /// <param name="intensity">The current intensity of the sun.</param>
    28.         /// <param name="weather">The current weather.</param>
    29.         /// <param name="time">The current time.</param>
    30.         public void Set(Vector3 position, Quaternion rotation, Color color, float intensity, Weather weather, float time)
    31.         {
    32.             sunData.currentRotation = rotation;
    33.             sunData.currentColor.Set(color);
    34.             sunData.currentIntensity = intensity;
    35.             currentWeather = weather;
    36.             currentTime = time;
    37.         }
    38.  
    39.         /// <summary>
    40.         /// Sun data to serialize.
    41.         /// </summary>
    42.         [Serializable]
    43.         public class SunData
    44.         {
    45.             /// <summary>
    46.             /// The current rotation of the sun.
    47.             /// </summary>
    48.             public Quaternion currentRotation;
    49.             /// <summary>
    50.             /// The current color of the sun.
    51.             /// </summary>
    52.             public jColor currentColor = new jColor();
    53.             /// <summary>
    54.             /// The current intensity of the sun.
    55.             /// </summary>
    56.             public float currentIntensity;
    57.  
    58.             /// <summary>
    59.             /// Serializable color class.
    60.             /// </summary>
    61.             [Serializable]
    62.             public class jColor
    63.             {
    64.                 /// <summary>
    65.                 /// Red.
    66.                 /// </summary>
    67.                 public float R;
    68.                 /// <summary>
    69.                 /// Green.
    70.                 /// </summary>
    71.                 public float G;
    72.                 /// <summary>
    73.                 /// Blue.
    74.                 /// </summary>
    75.                 public float B;
    76.                 /// <summary>
    77.                 /// Alpha.
    78.                 /// </summary>
    79.                 public float A;
    80.  
    81.                 /// <summary>
    82.                 /// Sets this instance from a Unity Color class instance.
    83.                 /// </summary>
    84.                 /// <param name="color"></param>
    85.                 public void Set(Color color)
    86.                 {
    87.                     R = color.r;
    88.                     G = color.g;
    89.                     B = color.b;
    90.                     A = color.a;
    91.                 }
    92.  
    93.                 /// <summary>
    94.                 /// Makes a Unity Color instance from this serialized package.
    95.                 /// </summary>
    96.                 /// <returns></returns>
    97.                 public Color ToColor()
    98.                 {
    99.                     return new Color(R, G, B, A);
    100.                 }
    101.             }
    102.         }
    103.     }
    104.  
    105.     /// <summary>
    106.     /// Weather types.
    107.     /// </summary>
    108.     [Serializable]
    109.     public enum Weather : int
    110.     {
    111.         /// <summary>
    112.         /// Sunny.
    113.         /// </summary>
    114.         None = 0,
    115.         /// <summary>
    116.         /// Foggy.
    117.         /// </summary>
    118.         LightFog = 1,
    119.         /// <summary>
    120.         /// Very foggy.
    121.         /// </summary>
    122.         DenseFog = 2,
    123.         /// <summary>
    124.         /// Light rain.
    125.         /// </summary>
    126.         LightRain = 3,
    127.         /// <summary>
    128.         /// Stormy conditions.
    129.         /// </summary>
    130.         RainStorm = 4,
    131.         /// <summary>
    132.         /// Light snow.
    133.         /// </summary>
    134.         LightSnow = 5,
    135.         /// <summary>
    136.         /// Blizzard.
    137.         /// </summary>
    138.         SnowStorm = 6
    139.     }
    I believe that the only workaround would be to remove all the packages and have all of the data fields exposed in the base DynamicData class but that seems so rudimentary. Are there any better options before I do this?
    Thanks
     
  2. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Serialize and deserialize your objects with BinaryFormatter and send the byte stream via remote procedrue call all to other clients.
    That's my code for custom object serialitazion:

    Code (CSharp):
    1. public static class SerializedPlayerDataView
    2. {
    3.     public static byte[] SerializeGamePeerInfo(GamePeerInfo peer)
    4.     {
    5.         BinaryFormatter binaryF = new BinaryFormatter();
    6.         using (MemoryStream memoryStream = new MemoryStream())
    7.         {
    8.             binaryF.Serialize(memoryStream,peer);
    9.  
    10.             return memoryStream.ToArray();
    11.         }
    12.     }
    13.  
    14.     public static GamePeerInfo DeserializeGamePeerInfo(byte[] dataStream)
    15.     {
    16.         using (MemoryStream memoryStream = new MemoryStream())
    17.         {
    18.             BinaryFormatter binaryF = new BinaryFormatter();
    19.  
    20.             memoryStream.Write(dataStream, 0, dataStream.Length);
    21.             memoryStream.Seek(0, SeekOrigin.Begin);
    22.  
    23.             return (GamePeerInfo)binaryF.Deserialize(memoryStream);
    24.         }
    25.     }
    26. }

    And how to use:


    Code (CSharp):
    1. public void sendSerializedData()
    2. {
    3.      myClass cusObject = new myClass();
    4.      cusObject.Value = 10; // example
    5.  
    6.      byte[] serializedObjectStream = SerializedView.Serialize(cusObject);
    7.  
    8.     view.RPC("GetStreamData",PhotonTargets.Others, (byte)serializedObjectStream);
    9. }
    10.  
    11. [PunRPC]
    12. private void GetStreamData(byte[] stream,photonMessageInfo senderInfo)
    13. {
    14.      myClass obj = SerializedView.Deserialize(stream);
    15.  
    16.      myValue = obj.Value; // use it example
    17. }
     
    EricBeato and Railon23 like this.
  3. KMKxJOEY1

    KMKxJOEY1

    Joined:
    Jan 2, 2013
    Posts:
    10
    Thanks for the code, but I would rather stay away from calling an remote procedure call every network tick. If I am unable to find a way to serialize my DynamicData class then I can always just expose all the fields and serialize it that way. I would not resort to an RPC.
     
  4. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    What is the problem about rpc?
     
    minpu likes this.
  5. KMKxJOEY1

    KMKxJOEY1

    Joined:
    Jan 2, 2013
    Posts:
    10
    There is no real 'problem' per say but RPC is more of an event based networking solution. The realtime stream from using OnPhotonSerializeView is intended to be invoked every frame so I would prefer to use the built-in Photon solution. Adding additional RPC's every network tick cannot be good for performance and bandwidth.
     
  6. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    You can sendNext the byte stream too by using OnPhotonSerializeView. OnPhotonSerializeView is not being called every frame, it's called only when your value changes. You can develop a system that will send RPC's only when it's needed.
     
  7. KMKxJOEY1

    KMKxJOEY1

    Joined:
    Jan 2, 2013
    Posts:
    10
    Unfortunately you are incorrect. OnPhotonSerializeView can be called every frame depending on the Observe Option you set in the PhotonView that observes the network component. Additionally, for a day cycle, an update does happen every frame. I would like to stay away from RPC's every frame.

    However, it may be possible to utilize your byte stream system on the OnPhotonSerializeView if Photon can serialize it.

    Thanks
     
  8. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Did you try register custom types?
     
  9. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    As I said you can still develop a system to lower the call rate to just few times per second
     
  10. KMKxJOEY1

    KMKxJOEY1

    Joined:
    Jan 2, 2013
    Posts:
    10
    I was able to register a custom type using a similar byte array system that you have shown. On referencing the Photon API, you must also register a custom type before attempting to serialize it on the Photon stream. This is what I did to get it to work right using OnPhotonSerializeView. Thanks.
     
  11. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    OnPhotonSerializeView gets called X times/sec. By default it's 10 times but it can be set with the sendRateOnSerialize value.
    A lower value is to be preferred, as it means less traffic and serialization.

    You can sync a day/night cycle with the shared serverTimestamp, the same way "rounds" are synced. You just need to know when the day started (expressed as shared server time).
    There are a few sample usages of that in the package.

    On type serialization: You can register your own types but in many cases it's way more effective, to add the corresponding int/string directly to the stream/RPC to be sent. A custom type makes sense when you have more complicated classes, which you need to use quite often. Keep in mind that a custom type adds 4 bytes to anything you actually want to send. For a bool, this would be quite some overhead.
     
  12. minpu

    minpu

    Joined:
    Dec 8, 2013
    Posts:
    3
    I have done as you said but there is a problem about using RPC. I have used 2 scenes, the first scene serialize custom objects and send to another client in the second scene. There is an error message like "Received RPC ... for viewID but this photonView does not exist! was remote PV..."
     
  13. minpu

    minpu

    Joined:
    Dec 8, 2013
    Posts:
    3
    What is "GamePeerInfo" in your code?