How to change the value of a struct member in an array of structs?

Hi,

I have a USTRUCT that I have for a cart, the player can add new struct in a cart that is an array of struct but I can’t manage to change the value of a struct member in that array of struct.

In BP I know I add to use the Set Struct Member and Set Array Element to achieve this but I can’t find their equivalent in C++ and my following code does not update the value (the for loop and if statement are working though)

for(FCartSaleItem ItemPresent : PlayerCartItem)
		{
			if(ItemPresent.ObjectMainID==Item->ObjectMainID&&ItemPresent.ObjectCurrentSelectedColorCollectionID==Item->ObjectColorCollectionID[Item->CurrentSelectedColorID]&&ItemPresent.ObjectCurrentSelectedSizeCollectionID==Item->ObjectSizeCollectionID[Item->CurrentSelectedSizeID])
			{
				ItemPresent.ObjectQuantityInCart++;
				
				UE_LOG(LogTemp,Warning,TEXT("Item is already present in the cart"));
				return;
			}
			else
			{
				ItemToAddToCart.ObjectMainID=Item->ObjectMainID;
				ItemToAddToCart.ObjectQuantityInCart=1;
				ItemToAddToCart.ObjectCurrentSelectedColorCollectionID=Item->ObjectColorCollectionID[Item->CurrentSelectedColorID];
				ItemToAddToCart.ObjectCurrentSelectedSizeCollectionID=Item->ObjectSizeCollectionID[Item->CurrentSelectedSizeID];
				ItemToAddToCart.ObjectBrandID=Item->ObjectBrandID;
				ItemToAddToCart.ObjectName=Item->ObjectName;
				ItemToAddToCart.ObjectDescription=Item->ObjectDescription;
				ItemToAddToCart.InventoryObjectType=Item->InventoryObjectType;
				ItemToAddToCart.InspectionObjectType=Item->InspectionObjectType;

				PlayerCartItem.Add(ItemToAddToCart);
		
				
			}
		}

Thanks in advance.

You’re iterating over the array by creating copies of each item. It’ll be far easier if you iterate over references to those items:

for (FMyStruct& StructRef : ArrayOfStructs)
{
	StructRef.Member = NewValue;
}

If you don’t want to change any items, you often iterate by const-ref instead.

Thanks, it works now

Thanks bro ! I’m so stupid I forgot to pass my struct by reference…