Serialization of nested classes

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

User avatar
Zelek
Posts: 36
Joined: 24 Jun 2010, 22:36

Serialization of nested classes

Postby Zelek » 06 Sep 2011, 02:58

Is there anything special that has to happen to enable nested serialization of objects?

I have a Party object on the server, and it contains an array of PartyMember objects. When I send the Party object to the client, Unity's OnExtensionResponse never gets triggered. I don't receive any exceptions on the client or server, and logging indicates that everything proceeded as planned. Sending just a PartyMember object seems to work fine.

I'm following the steps listed in the documentation and in Thomas's post on Sending/receiving classes. His example doesn't contain an array of serializable objects, though...is that allowed?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 07 Sep 2011, 10:06

Hmmm - got some more details on what you exactly are sending?

Also beware that the class serialization stuff doesnt allow for generics (yet?). So that might be your issue.

Else paste a few code snippets!

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
User avatar
Zelek
Posts: 36
Joined: 24 Jun 2010, 22:36

Postby Zelek » 08 Sep 2011, 06:00

The PartyMember object:

Code: Select all

public class PartyMember implements SerializableSFSType {
    public String name;
    public int portrait;
    public int type;

    // Necessary for serialization
    public PartyMember() {  }

    public PartyMember(String name, int portrait, int type) {
        this.name = name;
        this.portrait = portrait;
        this.type = type;
    }
}


Then I wanted to have a Party object which contained an array of PartyMembers:

Code: Select all

public class Party implements SerializableSFSType {
    // The list of PartyMembers in this Party
    public PartyMember[] partyMembers;

    // The maximum size of a Party
    public int MAX_SIZE = 10;

    // Necessary for serialization
    public Party() { }

    // Create a brand new party
    public Party(String name, int portrait, int type) {
        partyMembers = new PartyMember[MAX_SIZE];

        partyMembers[0] = new PartyMember(name, portrait, type);
    }
}


The ideal implementation for the partyMembers variable would have been a Hashmap<Integer, PartyMember> since the indices will often have gaps and not be sequential. Since your notes mentioned needing to use a Hashtable indexed by strings only, I thought an array would be more appropriate here.

After some fiddling, I got an error to show up in the server logs, mentioning that PartyMember[] did not implement SerializableSFSType (even though the PartyMember class did). Wasn't sure what to make of that, and ended up going with the less desirable Hashtable.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 20 Oct 2011, 14:26

Will put your example through some paces here as a test case.

Thanks

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 52 guests