question

madmojoman avatar image
madmojoman asked

CustomGameServer: Able to send List data to client?

I was hoping to be able to send a list of inventory items from the server to the client in response to a request from the client for it, only sending a small portion of a list of inventory items that the client needed to load some things properly. But a List of ItemInstance objects aren't able to be serialized/deserialized automatically with the MessageBase messages, it seems.

Do I need to write custom serialize/deserialize methods in order to pass that list of ItemInstance to the client, or is there an easier way to do that I just haven't been able to locate yet? That ItemInstance has a lot of data in it, so I didn't want to start writing one if there was something already available.

For reference (this fails because it can't have generic parameters, ie: List):

public class CoolDataMessage : MessageBase

{

public string thisTextGoesFine; // Good

public int thisNumberGoesFine; // Good

public List<ItemInstance> partialListOfInventoryItems; // Doesn't work

// Do I need to write my own serialize/deserialize methods here?

// Or is there some built-in way I could pass this partialListOfInventoryItems to the client properly?

}

Custom Game ServersPlayer Inventory
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

MessageBase just takes an array of bytes for the payload of the packet to be sent, so you'll want to serialize that into a string for transmission, and then deserialize it on receipt. There's no need to write you own serializer/deserializer, though - most engines (like Unity - check JsonUtility) have at least one library built-in for that.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

madmojoman avatar image madmojoman commented ·

I don't believe it supports Lists of objects like ItemInstance which have Lists and Dictionary objects included within them. I'll probably have to write a custom wrapper/helper at least to serialize/deserialize every single line of data in the ItemInstance to work with the ItemInstance objects, I guess. Ugh. Unless I'm missing some simple way of doing this that you didn't think of either.

0 Likes 0 ·
brendan avatar image brendan madmojoman commented ·

Hm. Actually, yes, reviewing their implementation, I'd have to correct that to say that JSON.Net would be your best bet. It doesn't look like JsonUtility has good support for Lists and Dictionaries.

0 Likes 0 ·
madmojoman avatar image madmojoman commented ·

I was just hoping there was some PlayFab helper that'd serialize/deserialize the List<ItemInstance> easily since the ItemInstance is Serializable. But I can't seem to find anything. So, I assume I have to loop through every single List and Dictionary and all the other data included for every ItemInstance in the List<> and then serialize them all and deserialize on the other side and then reconstruct the List of ItemInstance objects from that.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.