Cannot RPC array of custom type?

Options
Hey folks,

I was doing some coding on a scoreboard yesterday, and at one point I was trying to send an array of a custom class (Score) to a new player with an RPC. The score class consists of 2 ints and 1 string. This is the error I got:

hQ6zjcs.jpg

And here is the code that throws the error:

wDFUHPf.jpg

I tried implementing ISerializable and add attribute [Serializable] on the class, but this didn't solve the issue. Is there any way to send an array of custom class through RPC's, or do I have to find another solution to this?

Comments

  • shimauma
    Options
    I solved the problem by myself, by converting/serializing the array into JSON string and then sending that one across the RPC, and then deserializing it back into an array of Score.

    Code solution here:

    QVMeJaf.jpg

    Is this the way I should expect to solve these kinds of problems, or is there a way to send an actual array without having to manually serialize and deserialize it every time?
  • Tobias
    Options
    Photon does not support the ISerializable interface like Unity does. Instead, you have to register your Custom Type in our protocol and provide serialization and de-serialization methods to be used for it. Then you can send it through Photon.

    The PUN project has a CustomTypes class, which registers several Types of Unity. You likely can copy and paste some of this for your usecase.
    Keep in mind that custom types have some overhead over using basic types like int, etc. Example: If you send just a byte or bool as custom type, you end up with 4 bytes extra.
  • shimauma
    Options
    Thanks for the reply. I'll look into the CustomTypes.

    Do you know how to calculate how the size/how many bytes a custom type object (or any variable type with a value in it) generates/takes? I assume the size of a custom type varies depending on its contents...
  • Tobias
    Options
    Yes, the custom type size depends directly on what you put into it plus a fixed-size overhead. We have to send the length (4 bytes integer) plus 2 bytes of type-info per custom-type value to be able to read it again.