Search Unity

Serialising a List<class>

Discussion in 'Scripting' started by Oginecka, Sep 25, 2016.

  1. Oginecka

    Oginecka

    Joined:
    Sep 25, 2016
    Posts:
    7
    Code (CSharp):
    1.     [Serializable]
    2.     public class ItemContent : MessageBase
    3.     {
    4.         public string uuid;
    5.         public string owner_uuid;
    6.         public string owner_type;
    7.         public string item_name;
    8.         public string item_type;
    9.  
    10.         public int quantity;
    11.  
    12.         public int stat_1;
    13.         public int stat_2;
    14.         public int stat_3;
    15.         public int stat_4;
    16.         public int stat_5;
    17.         public int stat_6;
    18.         public int stat_7;
    19.         public int stat_8;
    20.         public int stat_9;
    21.         public int stat_10;
    22.  
    23.         public string text_1;
    24.         public string text_2;
    25.         public string text_3;
    26.         public string text_4;
    27.     }
    28.  
    29.     [Serializable]
    30.     public class ListHolder : List<ItemContent>
    31.     {
    32.     }
    33.  
    34.     [Serializable]
    35.     public class ItemContentList : MessageBase
    36.     {
    37.         [SerializeField]
    38.         public ListHolder itemlist;
    39.     }
    I am sending custom classes over a network. This works individually, but once I pack the classes into a List, it arrives empty on deserialisation.

    The above class "ItemContent" serialises and deserialises fine on its own.

    I then made a "ListHolder" class, extending a List<ItemContent>. This allowed me to avoid compile errors, as you can't serialise Lists directly. This class is instantiated in an ItemContentList class, which is then sent over the network.

    Debugging allows me to check the Count of the instantiated "ListHolder" before serialisation. This is correct. Upon deserialisation, the Count returns 0.

    Is it even possible to serialise stuff like this?
    What would be the alternative? I need to pack several "ItemContent" classes into one bundle, sending it over the network as a whole.
    I could of course send them over the network individually, but that seems messy.
     
  2. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    What do you mean by "you can't serialise Lists directly"? A class with a variable of type List<ItemContent> should serialize just fine.
     
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Its better to just go through your list of items, serialize them one by one and store that serialized info and then send them all over together. From what I remember a lust container cant be serialized at all (If anyone else has a more indepth knowledge feel free to correct me) so you would need to write your own version to allow supporting it or just do each element one at a time.

    With that in mind, if you write a couple of functions in your list holder to handle going through each element and serializing it and one for doing the reverse you should be able to keep the code generally clean.
     
  4. Dennis59

    Dennis59

    Joined:
    Jan 8, 2013
    Posts:
    66
    I agree with lordconstant, you have to send one item at a time from the list and then rebuild it on the receiving end. You didn't say what networking system you are using. There are some that handle lists in various ways. As an example UNet has a SyncList to serialize data from server to client. This is probably similar although I've not used it. And UNet, as with other networking systems, has an interface to allow you to create custom serialization functions so that you can handle custom content.
     
  5. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    Alternatively you can serialize an array, so you could have a method that takes your list item and converts it into the Array you need to send. Then you can put it back into the list on the other side?
     
    lordconstant likes this.