Author Topic: Issue with serialization over RFC?  (Read 1689 times)

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Issue with serialization over RFC?
« on: May 12, 2014, 05:32:10 PM »
Hello,
I'm trying to send a custom class via a broadcast, and get it via a RFC method.

Here is the class:
  1. [Serializable]
  2. public class GameData : IBinarySerializable
  3. {
  4.         public int id { get; private set; }
  5.         public string name { get; private set; }
  6.         public string spriteName { get; private set; }
  7.         public int hostID;
  8.  
  9.         public GameData(int id, string name, string spriteName)
  10.         {
  11.                 this.id = id;
  12.                 this.name = name;
  13.                 this.spriteName = spriteName;
  14.  
  15.                 hostID = int.MinValue;
  16.         }
  17.  
  18.         public void Serialize(BinaryWriter writer)
  19.         {
  20.                 writer.Write(id);
  21.                 writer.Write(name);
  22.                 writer.Write(spriteName);
  23.                 writer.Write(hostID);
  24.         }
  25.  
  26.         public void Deserialize(BinaryReader reader)
  27.         {
  28.                 id = reader.ReadInt();
  29.                 name = reader.ReadString();
  30.                 spriteName = reader.ReadString();
  31.                 hostID = reader.ReadInt();
  32.         }
  33. }
  34.  

and in an empty scene, with just the following script attached to the camera (and a TNObject with ID 1):
  1. public class Test2 : TNBehaviour
  2. {
  3.         void Start ()
  4.         {
  5.                 Debug.Log(TNManager.StartUDP(12345));
  6.         }
  7.        
  8.         void Update ()
  9.         {
  10.                 if (Input.GetKeyDown(KeyCode.S))
  11.                 {
  12.                         GameData gameData = new GameData(5, "Hello", "World");
  13.                         gameData.hostID = 25;
  14.                         tno.BroadcastToLAN(12345, "OnGameCreatedRFC", gameData);
  15.                 }
  16.         }
  17.  
  18.         [RFC]
  19.         void OnGameCreatedRFC(GameData gameData)
  20.         {
  21.                 Debug.Log(gameData);
  22.         }
  23. }
  24.  

Calling Debug.Log(gameData) return null.

Any idea why?
Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue with serialization over RFC?
« Reply #1 on: May 13, 2014, 09:33:03 AM »
TNet doesn't care for your parametrized constructor. :P

Classes must have a normal no-parameter constructor, and what you're trying to make serializable must be fields, not properties.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Issue with serialization over RFC?
« Reply #2 on: May 13, 2014, 10:01:19 AM »
Thanks! Working like a charm :)
« Last Edit: May 14, 2014, 03:08:12 AM by nah0y »