# Replicating custom subobject which is UPROPERTY of ActorComponent

**URL:** <https://forums.unrealengine.com/t/replicating-custom-subobject-which-is-uproperty-of-actorcomponent/489340>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [September 30, 2021, 12:11pm UTC](https://forums.unrealengine.com/t/replicating-custom-subobject-which-is-uproperty-of-actorcomponent/489340 "2021-09-30T12:11:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TJB461](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TJB461](https://forums.unrealengine.com/u/TJB461)\
**Post date:** [September 30, 2021, 12:11pm UTC](https://forums.unrealengine.com/t/replicating-custom-subobject-which-is-uproperty-of-actorcomponent/489340/1 "2021-09-30T12:11:31Z")

</div>

Basically the title. I have an `InventoryComponent`, which holds a pointer to a custom `UInventory` class. This inventory class contains a TArray which should be replicated. However, the pointer to the `UInventory` is always NULL. No idea why.

Code snippets:

UInventory:

```
UCLASS()
class UInventory : public UObject
{
	GENERATED_BODY()

private:

	UPROPERTY(ReplicatedUsing=OnRep_InventoryArray)
		TArray<FInventoryEntry> mInventoryArray;

	UPROPERTY()
		TMap<FName, int32> mLookupCache;

	UFUNCTION()
		void RebuildCache();

	UFUNCTION()
		void OnRep_InventoryArray();

public:
	UInventory();

	TTuple<EChangeGroupStatus, TArray<EChangeStatus>> ModifyGroupOfEntries(const TArray<FInventoryEntry>& inventoryChanges);

	TTuple<EChangeGroupStatus, TArray<EChangeStatus>> ModifyGroupOfEntries(const TMap<FName, int32>& inventoryChanges);

	void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

	virtual bool IsSupportedForNetworking() const override { return true; }

	UFUNCTION(Category = "Networked Inventory")
		EAddStatus AddNewEntry(const FInventoryEntry& entry);

	UFUNCTION(Category = "Networked Inventory")
		EChangeStatus ModifyEntry(const FInventoryEntry& entryChange);

	UFUNCTION(Category = "Networked Inventory")
		TArray<ERemovalStatus> RemoveGroupOfItems(const TArray<FName>& itemsToRemove);

	UFUNCTION(Category = "Networked Inventory")
		ERemovalStatus RemoveItem(const FName itemCode);

	UFUNCTION(Category = "Networked Inventory")
		int32 GetQuantityFor(const FName itemCode) const;

	UFUNCTION(Category = "Networked Inventory")
		bool Contains(const FName itemCode) const;

	UFUNCTION(BlueprintCallable, Category = "Networked Inventory")
		int32 Num() const;

	UFUNCTION(BlueprintCallable, Category = "Networked Inventory")
		FString ToString() const;
};

```

UInventoryComponent:

```
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class NETWORKEDINVENTORY_API UInventoryComponent : public UActorComponent, public IInventoryInterface
{
	GENERATED_BODY()

private:
	UPROPERTY(Replicated, meta = (AllowPrivateAccess = true))
		UInventory* mInventory;

public:
	UInventoryComponent();

	void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

	virtual bool IsSupportedForNetworking() const override { return true; }

	virtual bool ReplicateSubobjects(UActorChannel* Channel, FOutBunch* Bunch, FReplicationFlags* RepFlags) override;

	//virtual void OnSubobjectCreatedFromReplication(UObject* NewSubobject) override;

	UFUNCTION(Category = "Networked Inventory")
		virtual void ModifyInventory(const TArray<FInventoryEntry>& inventoryChanges) override;

	UFUNCTION(Category = "Networked Inventory")
		virtual void AddItemsToInventory(const TArray<FInventoryEntry>& inventoryChanges) override;

	UFUNCTION(Category = "Networked Inventory")
		virtual void RemoveItemsFromInventory(const TArray<FInventoryEntry>& inventoryChanges) override;

	UFUNCTION(BlueprintCallable, Category = "Networked Inventory")
		virtual FString ToString() const override;

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

		
};

// Some definitions from the .cpp file

UInventoryComponent::UInventoryComponent()
{
	// No need to tick.
	PrimaryComponentTick.bCanEverTick = false;

	mInventory = CreateDefaultSubobject<UInventory>(TEXT("Inventory"));
}

void UInventoryComponent::GetLifetimeReplicatedProps(TArray <FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	DOREPLIFETIME(UInventoryComponent, mInventory);
}

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

	//const int32 inventoryObjectID = 0;
	//if (Channel->KeyNeedsToReplicate(inventoryObjectID, ???RepKey))
	//{
		wroteToChannel |= Channel->ReplicateSubobject(mInventory, *Bunch, *RepFlags);
	//}

	return wroteToChannel;
}

```

---

<div class="post-metadata">

**Author:** ![Jacob\_Odell](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jacob_odell/32/724420_2.png) [@Jacob\_Odell](https://forums.unrealengine.com/u/Jacob_Odell)\
**Post date:** [August 16, 2023, 2:42pm UTC](https://forums.unrealengine.com/t/replicating-custom-subobject-which-is-uproperty-of-actorcomponent/489340/2 "2023-08-16T14:42:15Z")

</div>

Were you able to find a resolution to this issue? I am having essentially the same issue but am unsure if my approach is wrong or if I’m not setting replication up correctly.
