How to serialize a Struct in Photon Realtime

Options
Hi,
I'm making a game in UE4 using Photon Realtime. I need to serialize a struct which contains the vanity of the player. I found some documentation that explains how to do this, but all is in photon PUN with Unity and it does not serve me.

Can somebody explain how to serialize an struct in Photon Realtime for UE4

The struct is this:
struct PlayerVanity
{
	int32 PlayerNr;
	JString name;
	int32 meshID;
	int32 skinID;
	int32 hairID;
	int32 clothID;
};

Best Answer

  • Kaiserludi
    Kaiserludi admin
    Answer ✓
    Options
    Hi @Davis.

    You have the following 4 options:



    Option a)
    Copy all members of the struct, that need to be transferred into a Common::Object-array, send that array through Photon and copy all values back out of it into a new instance of the struct on the receiving side.



    Option b)
    Use a union of your struct and an accordingly sized byte-array like shown in the following example code:
    struct CustomStruct
    {
    	int member1;
    	short member2;
    	long long member3;
    };
    
    union CustomUnion
    {
    	CustomStruct type;
    	unsigned char arr[sizeof(CustomStruct)];
    };
    
    	CustomUnion u;
    	u.type.member1 = 1;
    	u.type.member2 = 2;
    	u.type.member3 = 3;
    	mClient.opRaiseEvent(reliabilityFlag, u.arr, sizeof(u.arr), eventCode);
    

    Note that the union-approach only works if all members of your struct are POD types.
    So it does not work for your example struct, as it contains a class member variable (the JString instance 'name')



    Option c)
    Another approach would be to serialize your type into a byte-array and to pass that array to Photon.
    Then deserialize it back to the original struct on the receiving side.
    When using this approach you have to write proper serialization and derserilization functions for each of the structs that you want to transfer through Photon.



    Option d)
    You could let your custom structs inherit from ExitGames::Common::CustomType<typeCode> so that you can pass the custom struct itself to opRaiseEvent().

    For more information on the custom type APIs please see the API reference for CustomType at https://doc-api.photonengine.com/en/cpp/current/a04866.html, for CustomTypeBase at https://doc-api.photonengine.com/en/cpp/current/a04870.html and for CustomTypeFactory at https://doc-api.photonengine.com/en/cpp/current/a04874.html.

    pathToPhotonClientSDK\Demos\demo_typeSupport contains the class SampleCustomType as an example implementation of a custom type.

Answers

  • Kaiserludi
    Kaiserludi admin
    Answer ✓
    Options
    Hi @Davis.

    You have the following 4 options:



    Option a)
    Copy all members of the struct, that need to be transferred into a Common::Object-array, send that array through Photon and copy all values back out of it into a new instance of the struct on the receiving side.



    Option b)
    Use a union of your struct and an accordingly sized byte-array like shown in the following example code:
    struct CustomStruct
    {
    	int member1;
    	short member2;
    	long long member3;
    };
    
    union CustomUnion
    {
    	CustomStruct type;
    	unsigned char arr[sizeof(CustomStruct)];
    };
    
    	CustomUnion u;
    	u.type.member1 = 1;
    	u.type.member2 = 2;
    	u.type.member3 = 3;
    	mClient.opRaiseEvent(reliabilityFlag, u.arr, sizeof(u.arr), eventCode);
    

    Note that the union-approach only works if all members of your struct are POD types.
    So it does not work for your example struct, as it contains a class member variable (the JString instance 'name')



    Option c)
    Another approach would be to serialize your type into a byte-array and to pass that array to Photon.
    Then deserialize it back to the original struct on the receiving side.
    When using this approach you have to write proper serialization and derserilization functions for each of the structs that you want to transfer through Photon.



    Option d)
    You could let your custom structs inherit from ExitGames::Common::CustomType<typeCode> so that you can pass the custom struct itself to opRaiseEvent().

    For more information on the custom type APIs please see the API reference for CustomType at https://doc-api.photonengine.com/en/cpp/current/a04866.html, for CustomTypeBase at https://doc-api.photonengine.com/en/cpp/current/a04870.html and for CustomTypeFactory at https://doc-api.photonengine.com/en/cpp/current/a04874.html.

    pathToPhotonClientSDK\Demos\demo_typeSupport contains the class SampleCustomType as an example implementation of a custom type.
  • Davis
    Options
    Ohh thanks a lot for this information @Kaiserludi .
    In the path you told me to see the examples of custom types I folllowed some examples and was able to make a function to pass the my struct. Thank you very much, I was going crazy.