Search Unity

Merging stackable Items after Transaction ?

Discussion in 'Game Foundation' started by dasparadoxon, Jul 11, 2021.

  1. dasparadoxon

    dasparadoxon

    Joined:
    Aug 16, 2014
    Posts:
    11
    Hi,

    since I have posted two of my feedback question allready in two other posts, I will just write this one here : If you do a transaction and you buy an item, its added as normal item, even when it is a stackable item. So you have to merge the item to the allready existing item with a stack. (in this case unlimited stack).

    I did that like that :

    Code (CSharp):
    1.     public void MergeAllVegetableSeedsFromTransactionIntoOnePile(Vegetables vegetableBought)
    2.     {
    3.         // find the first in the list (main stack)
    4.         // go through all the rest, add them to main stack, delete them
    5.  
    6.         InventoryItemDefinition definition;
    7.  
    8.         List<UnityEngine.GameFoundation.InventoryItem> vegetableSeedsFindResultArray;
    9.  
    10.         string vegetableSeedString = vegetableBought.ToString();
    11.  
    12.         definition = GameFoundationSdk.catalog.Find<InventoryItemDefinition>(vegetableSeedString.ToLower() + "seed");
    13.  
    14.         dbg("Trying to find definition for :" + vegetableSeedString);
    15.  
    16.         vegetableSeedsFindResultArray = new List<UnityEngine.GameFoundation.InventoryItem>();
    17.  
    18.         int countResult = GameFoundationSdk.inventory.FindItems(definition, vegetableSeedsFindResultArray);
    19.  
    20.         long newQuantity = 0;
    21.  
    22.         UnityEngine.GameFoundation.InventoryItem firstItem = null;
    23.  
    24.         StackableInventoryItem quantityItem = null;
    25.  
    26.         if (countResult > 0)
    27.         {
    28.             int indexCount = 0;
    29.  
    30.             foreach(UnityEngine.GameFoundation.InventoryItem item in vegetableSeedsFindResultArray)
    31.             {
    32.  
    33.                 quantityItem  = (StackableInventoryItem)item;
    34.  
    35.                 newQuantity  += quantityItem.quantity;
    36.  
    37.                 if (indexCount != 0)
    38.                     GameFoundationSdk.inventory.Delete(item);
    39.                 else
    40.                     firstItem = item;
    41.  
    42.                 indexCount++;
    43.  
    44.             }
    45.         }
    46.  
    47.         StackableInventoryItem itemToSetFinalQuantity = (StackableInventoryItem)firstItem;
    48.  
    49.         itemToSetFinalQuantity.SetQuantity(newQuantity);
    But could not be a better way inside the framwork for this that does this automatically ?

    Also two small things :

    1) It would be very handy if the names of a property could be marked and copied in the clipboard in the Item Inventory Panel. (yes, I could use code generation, but just saying)

    2) (I found a typo on tutorial 10 "But the while version is better for perforance because it doesn't")
     
    erika_d likes this.
  2. erika_d

    erika_d

    Unity Technologies

    Joined:
    Jan 20, 2016
    Posts:
    400
    Hi @dasparadoxon,

    Thanks for your feedback/feature request on automated merging of stackable items upon purchase. It's a topic we discussed when initially introducing stackable items, but decided to go with the more simple create a new stack option for the first iteration, to allow developers the flexibility of choosing what behavior they want vs being opinionated and choosing for them. I'll make note of the request for more automation surrounding this flow though so when we're next iterating on this feature we can take a second look at it!

    Good callout on letting the keys of the properties be copyable, I will also make a note about that for our next editor pass.

    Also thanks for letting us know about the typo in the tutorial. :)
     
    dasparadoxon likes this.
unityunity