# Only Replicate When Requested? (C++)

**URL:** <https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, question\
**Created:** [July 7, 2024, 7:34pm UTC](https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711 "2024-07-07T19:34:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WellNow](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WellNow](https://forums.unrealengine.com/u/WellNow)\
**Post date:** [July 7, 2024, 7:34pm UTC](https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711/1 "2024-07-07T19:34:55Z")

</div>

Is there a way to replicate an array only when a player opens an inventory? I don’t think it’s necessary to replicate every inventory to every client all the time; currently how my system is working. Basically, the client shouldn’t know or care what a storage box or loot crate has until the player accesses its inventory.

---

<div class="post-metadata">

**Author:** ![Auran131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Auran131](https://forums.unrealengine.com/u/Auran131)\
**Post date:** [July 8, 2024, 3:09am UTC](https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711/2 "2024-07-08T03:09:07Z")

</div>

Relevancy already does this to some degree, ie if the player is too far away it wont replicate.

> **[Actor Relevancy and Priority in Unreal Engine | Unreal Engine 5.2...](https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/Networking/Actors/Relevancy/)**
>
> Information about Actor relevancy and priority

---

<div class="post-metadata">

**Author:** ![WellNow](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WellNow](https://forums.unrealengine.com/u/WellNow)\
**Post date:** [July 8, 2024, 9:54pm UTC](https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711/3 "2024-07-08T21:54:21Z")

</div>

Thanks, Auran13. I’ve been playing with the IsNetRelevantFor function with some success. It seems once I return True from the IsNetRelevantFor override for any PlayerController it’s stuck in replication and I can’t seem to stop it.

I’ve tested this running as a client in PIE. The server would add 14,000 items to the array. I’d have the client check and see 0, which I expected since I’m not replicating. Then I’d return True from the IsNetRelevantFor override and check again to see 14,000. So far so good. I’d then return False from IsNetRelevantFor. I’d have the server then add another 500 and while still returning False check again and now see 14,500, which I wasn’t expecting as the inventory shouldn’t, or so I thought, be replicating.

The reason for so many elements in an array is because players can place down storage boxes, workbenches, drop bags, etc, and store various resources. The problem is, with everything replicating I often run into “Attempted to send bunch exceeding max allowed size”. This doesn’t strictly happen with one massive inventory. This also happens if too many smaller inventories are replicating to a client, hence the need to only replicate on request.

The code is nothing fancy. Regarding DOREPLIFETIME, I’ve tried both and got the same result.

```auto
// UInventoryComponent : ActorComponent
//H
UPROPERTY(Replicated) TArray<UWorldItem*> Collection;

//CPP
void UInventoryComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	//DOREPLIFETIME(UInventoryComponent, Collection);
	DOREPLIFETIME_CONDITION(UInventoryComponent, Collection, COND_Custom);
}

// AStorageActor : AActor
//H
UPROPERTY(Replicated) UInventoryComponent* Inventory;

//CPP
void AStorageActor::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	//DOREPLIFETIME(AStorageActor, Inventory);
	DOREPLIFETIME_CONDITION(AStorageActor, Inventory, COND_Custom);
}

bool AStorageActor::IsNetRelevantFor(const AActor* RealViewer, const AActor* ViewTarget, const FVector& SrcLocation) const
{
	if (const ASonaController* PlayerController = Cast<ASonaController>(RealViewer))
		return AccessedBy.Contains(PlayerController);
	return false;
}

void AStorageActor::ToggleInventory(ASonaController* PlayerController)
{
	if(HasAuthority()) Internal_ToggleInventory(PlayerController);
	else Server_ToggleInventory(PlayerController);
}

void AStorageActor::Server_ToggleInventory_Implementation(ASonaController* PlayerController)
{
	Internal_ToggleInventory(PlayerController);
}

void AStorageActor::Internal_ToggleInventory(ASonaController* PlayerController)
{
	if(PlayerController)
	{
		if(AccessedBy.Contains(PlayerController))
			AccessedBy.Remove(PlayerController);
		else AccessedBy.AddUnique(PlayerController);

		ForceNetUpdate();
	}
}

```

AStorageActor replication details

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/2/b/a2b7bf5e9b8c322fc41a4874b05f2e8f372d29d5.png)

---

<div class="post-metadata">

**Author:** ![PREDALIEN](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/predalien/32/1194670_2.png) [@PREDALIEN](https://forums.unrealengine.com/u/PREDALIEN)\
**Post date:** [July 8, 2024, 10:41pm UTC](https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711/4 "2024-07-08T22:41:32Z")

</div>

It will probably suit you [DOREPLIFETIME\_ACTIVE\_OVERRIDE](https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/Networking/Actors/Properties/Conditions/)

The second option is to send an RPC event request to the server and receive the result via a client event.  
In this case, the variable should not be replicated on its own.

---

<div class="post-metadata">

**Author:** ![WellNow](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WellNow](https://forums.unrealengine.com/u/WellNow)\
**Post date:** [July 10, 2024, 11:31pm UTC](https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711/5 "2024-07-10T23:31:36Z")

</div>

Thanks for the guidance. I may have been over complicating things a bit. After a little more tinkering and testing I believe I have it working. I’m just using DOREPLIFETIME for both the storage actor and inventory component. In my character class I have a null inventory component pointer marked as replicated, also using DOREPLIFETIME. When I need to access a specific inventory, I’ll call a server RPC from the character to set the inventory pointer I want to access. After that, client side sees all the items. I also don’t get that “Attempted to send bunch exceeding max allowed size” error running the same 14,000 item test, even with switching to another inventory.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [July 11, 2024, 5:59am UTC](https://forums.unrealengine.com/t/only-replicate-when-requested-c/1929711/6 "2024-07-11T05:59:46Z")

</div>

> [@Warning: SendBunch: Reliable partial bunch overflows reliable buffer! \[UChannel\]](https://forums.unrealengine.com/t/warning-sendbunch-reliable-partial-bunch-overflows-reliable-buffer-uchannel/468288/3):
>
> How much data are you trying to replicate? The maximum amount is 65.536 bytes if I remember correctly. There is also a limit for the maximum array size, which is also 65.536. If you are below these values, it might be that you have not configured your project to make use of the maximum values. There are two settings in the ProjectSettings → Engine → Network called “MaxArraySize” and “MaxArrayMemory”. You could turn those up to max. Alternatively, add the following lines to your DefaultEngi…
