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.

Hello, I’ve tried to use approach you’ve mentioned but I faced a problem. For example I have Component and UObject* member inside:
UTestObject* testObject;

Inside UTestObject I just have a simple field like this:
UPROPERTY(Replicated)
int m_currentAmount = 0;

I also implemented both IsSupportedForNetworking and GetLifetimeReplicatedProps functions.

Then on both Server/Client in begin play I create testObject via NewObject. Then at somepoint of time I made an RPC call and change m_currentAmount on Server and I don’t receive this update on client.
However, if I change pointer itself, for example made something like this:
testObject = nullptr;
I will get an update on Client. Seems like it only replicates pointer by itself, but not the data inside pointer.
Do you know how to fix this? Thanks

did you add?

DOREPLIFETIME(UTestObject, m_currentAmount);

When you do replication, you only create object on the server. You are surely replicating only the pointer, not the object pointed.

So you are 2 distincts objets (client/server) but one pointer replicated. When you set null the pointer, the change is replicated, as expected.

The pointer and the object pointed are not the same thing.

PS : This post is old. You don’t need to replicate subobject like this anymore.

whats the new method?

1 Like