Download

How to replicate a UObject property inside a struct?

Hi !

I’m discovering how to replicate UObject contained in AActor (following this post Replicating UObjects: Building a Flexible Inventory System – James Baxter). My inventory system is a slot UObject contained in a UStruct itself contained in an ActorComponent. Something like that :

InventoryStruct =
{
    BackpackStruct = 
    {
         TArray<USlot> slotList,
    }
}

InventoryStruct is a UProperty of the InventoryComponent. Each slot have as Outer the InventoryComponent.

The struct is perfect to organize and initialize all slots (each substruct have an init function). Is there any way to do this UObject replication with the slot ?

Also based on what I found on the web, nobody agrees on the best way to make an inventory replicated… Some people say to make AActor item/slot to replicate them, other say that replicate high number of AActor isn’t a good idea for the network performance… Our game is survival multiplayer (like Ark), there will be potentially a ton of item in their inventory, what way would be optimal to reduce network usage ?

Hey, I just solved almost the same problem as you are attempting to!

I think you can mark the InventoryStruct, BackpackStruct and slotList as replicated
then in your ActorComponent’s ReplicateSubobjects()

// make sure to register your replicate vars in GetLifetimeReplicatedProps

bool UYourActorComponent::ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags)
{
	bool bWroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags);

    // this replicates the TArray
	bWroteSomething |= Channel->ReplicateSubobjectList(InventoryStruct.BackpackStruct.slotList, *Bunch, *RepFlags);

    // this replicates the actual TArray content
	for (auto* Slot : slotList)
	{
		bWroteSomething |= Channel->ReplicateSubobject(Slot, *Bunch, *RepFlags);
	}

	return bWroteSomething;
}

The code might have some syntax error or something since I am writing it top of my head.

Here is another good link :

Hope it helps!

Thanks ! I finally abandoned the struct to be able to continue but I will see if it works.