TMap not working on client in online/replication

Recently I saw that TMap was not replicated for Unreal, but if I dont want to replicate it, I just want to make calculations with the TMap structure and then if I want to replicate I will move the data to a particular structure. But I got problems doing the GetKeys() of my map, it says 0 but its 60, so I dont know what to do because when I try in standalone I get the right num.

Thanks in advance!

need to see your code.

1 Like

Are you talking about size?

TArray<int> Keys;
MyMap.GetKeys(Keys);

if (Keys.Num()==0) return;

Or are you talking about values?

Keys[i] = 60

It should be the same everywhere

Depends on how and where you are adding to the TArray. Replication is only Server → Client.

1 Like

Yes you’re right.

I was also too brief. I meant in Editor and Standalone.

basically I have an inventory state Tmap of FVectro2D and bool in Lyra, here the length is 60

void ULyraInventoryManagerComponent::DefaultInventoryState()
{
	float GridX = 0.f;
	float GridY = 0.f;
	
	for (int i = 0; i < InventorySize; i++)
	{
		InventoryState.Add(FVector2D(GridX, GridY), false);
		
		if(GridX == InventoryWidth-1)
		{
			GridX = 0.f;
			GridY++;
		}
		else
		{
			GridX++;
		}
	}

	TArray<FVector2D> ItemLocations;
	InventoryState.GetKeys(ItemLocations);

	// LENGTH == 60
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("LENGTH %d"), ItemLocations.Num()));
}

when I add an item the InventoryState says that its empty

ULyraInventoryItemInstance* ULyraInventoryManagerComponent::AddItemDefinition(TSubclassOf<ULyraInventoryItemDefinition> ItemDef, int32 StackCount)
{
	ULyraInventoryItemInstance* Result = nullptr;
	if (ItemDef != nullptr)
	{
		Result = InventoryList.AddEntry(ItemDef, StackCount);

		const UEO_InventoryFragment_Icon* Fragment = Cast<UEO_InventoryFragment_Icon>(Result->FindFragmentByClass(UEO_InventoryFragment_Icon::StaticClass()));

		if (Fragment)
		{
			bool bSuccess = false;
			int32 Remaining = StackCount;

			TArray<FVector2D> ItemLocations;
			InventoryState.GetKeys(ItemLocations);

			// length == 0
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("LENGTH in AddItemDefinition %i"), ItemLocations.Num()));
				
			AddItemFromFloor(Fragment, Result, bSuccess, Remaining);

			if (Remaining != 0)
			{
				Result->AddStatTagStack(TAG_Lyra_Inventory_Item_Count, Remaining);
			}
		}
		if (IsUsingRegisteredSubObjectList() && IsReadyForReplication() && Result)
		{
			AddReplicatedSubObject(Result);
		}
	}
	return Result;
}```

sometimes it says 60 and sometimes it says 0, like I dont know whats going on. But it is more often to see the 0 than the 60. The few times the right value appears it is in the first execution of the game once u open the engine

I think you have a context problem (you probably have some values ​​on the server and others on the client).

Use HasAuthority() function and handles data on one side only. (On the server if you are handling replicated variables). The client can only read.