Best practices to replicate an inventory (multiplayer)

Just in case anyone comes across this, the way I handle this type of system is more of a modular approach.

  1. Create an Actor Component for the Inventory (UInventoryComponent) and add #2.
  2. Create a UObject to act as a Item Container (USpatialInventoryContainer).
  3. Create a UObject for UItemObject_Base.
  4. Add TArray of type UItemObject_Base.
  5. Create all of your Item Objects parented to UItemObject_Base.
  6. Make a function in the Inventory Component to Add Item, and instead
    of passing in the UItemObject_Base child, you serialize the item into a struct with a uint8 byte array, and Item Class (Classes are always the same on the Client as the Server).
  7. Have the Client build the Item from the Serialized byte array.

Server only handles the transfer of the Byte array. Not pointers, and uses the
Inventory Component as a wrapper to the Owning Actors network channel.

This method can also be called to Synchronize any existing UObject, but not every frame.

For example, on a network test I can create 10,000 Items in 1500ms, 60 in about 9-14ms (on LAN). Please do not try this in blueprint. The serialization isn’t exposed by default, and since it is virtualized, it will make the server calls hang at scale. So so long as you control the flow to only replicate what is necessary, you shouldn’t have a problem with performance. (We use item stacking, not just for Play convenience, but to reduce bandwidth on the server.)

Be cautious about Inventory size.