UObject Serialization to pass as RPC Parameter

I’ve seen a lot of posts about sending UObjects as RPC parameters for instance as a data class. It took me a while to figure out exactly how to do it, and there is a lot of bad (wrong) information here in the forums.

Here is what worked for me.

To serialize a UObject:

  1. #include “Serialization/ObjectWriter.h”
  2. Create a TArray of uint8 ( in this example I’ll call it Bytes)
  3. Create an FObjectWriter and in the constructor pass in the UObject to serialize Reference and the TArray you created in step 2.
    IE: FObjectWriter ObjectWriter = FObjectWriter(UObjectRef, Bytes);

That’s it for serialization. The Bytes array contains the serialized UObject. NOTE!!! Any properties you want serialized have to be UPROPERTIES. They won’t serialize otherwise.

To send through an RPC call you could probably send the TArray, but I packaged it up in a USTRUCT and sent that.

On the receiving side it isn’t any harder…

  1. #include “Serialization/ObjectReader.h”
  2. Create the same type of UObject you serialized (I used NewObject(Outer)
  3. Create the FObjectReader and pass in the new UObject and the Bytes Tarray you sent through the RPC
    IE: FObjectReader ObjectReader = FObjectReader(NewUObject, Bytes);

That NewUObject should now be populated with the data you sent across the network.

Other uses for serialization could be saving data. You would save the TArray Bytes, then on load populate the new objects.

Anyway, hope this helps! It took me a day or so of sorting through crap to find the correct answer, which ended up being a bunch of pieced together info. So, I figured I’d consolidate it down into 1 topic.

Good luck!

6 Likes

Detailed description. thanks a lot