Show the UE error message.
FinventoryStruct Item = ItemToAdd;
Int32 index = 0;//need it intialized here for use, so you can use it thru out this function.
Item.InvItemInfo = ItemToAdd.InvItemInfo;
Item.InventoryItemQuantity = 0;
if(!Inventory.FindLast(Item, Index))
{
Index = Inventory.AddUnique(Item);// that should get you your index after you have added it.
}
else
{
//IF if(!Inventory.FindLast(Item, Index) RETURNS A INDEX THEN YOU WILL NOT NEED THE FOR
for(int32 i=0; i <Inventory.num(); i++ )
{
//might need if(Inventory[i].Get() == Item)
if(Inventory[i] == Item)
{
Index = i;
break;
}
}
Inventory[Index].InventoryItemQuantity += ItemToAdd.InventoryItemQuantity;
}
Will that work? If you space the code lines with 4 to 5 spaces at the start of each line it will post the code like mine above
This image doesn’t show anything.
void APlayerCharacter::AddToInventory(FInventoryStruct ItemToAdd)
{
int Index = -1;
FInventoryStruct Item;
Item.InvItemInfo = ItemToAdd.InvItemInfo;
Item.InventoryItemQuantity = ItemToAdd.InventoryItemQuantity;
for (int i = 0; i < Inventory.Num(); i++)
{
if (Inventory[i].InvItemInfo.InventoryItemID == Item.InvItemInfo.InventoryItemID && Inventory[i].InvItemInfo.InventoryItemRarity == Item.InvItemInfo.InventoryItemRarity && Inventory[i].InvItemInfo.MaxStackSize == Item.InvItemInfo.MaxStackSize)
{
Index = i;
}
}
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, FString("INDEX: " + FString::FromInt(Index)));
}
if (Index == -1)
{
Inventory.Add(ItemToAdd);
}
else
{
Inventory[Index].InventoryItemQuantity += Item.InventoryItemQuantity;
}
}
I solved the problem. It consisted in the fact that the == operator does not work for structures. Therefore, you cannot use the FindLast function. I wrote a new function using a for loop, similar to the one you have.
i was thinking about this this morning and i think you could just write it like this. You would need to test this but i am pretty sure this will do the same thing, just less code.
void APlayerCharacter::AddToInventory(FInventoryStruct ItemToAdd)
{
int32 Index = -1;
if(ItemToAdd)
Index = Inventory.AddUnique(ItemToAdd);//should only add it if it is not in the array.
if(Index != INDEX_NONE)//you found it, it
{
Inventory[Index].InventoryItemQuantity += ItemToAdd.InventoryItemQuantity;
}
}
Glad to see you got it working.