Search code examples
arraysfor-loopunity-game-engineforeachinventory

Unity - Adding multiple array parameters in method (for adding multiple items)


I'm trying to achieve a method that I can call to create multiple items

This is the method I'm trying to get it to work

public void AddMultipleItems(string[] itemKey, int[] amount)
{

    for (int i = 0; i < itemKey.Length; i++)
    {
        Item item;
        item = ItemCollection[itemKey[i]];

        for (int x = 0; x < amount.Length; x++)
        {
            if (inventory.CanAddItem(item, amount[x]) == true)
            {
                inventory.AddItem(item.GetCopy());
            }
            else if (inventory.CanAddItem(item, amount[x]) == false)
            {
                Debug.Log("Inventory Full");
                break;
            }

        }
    }

}

And then this method will be called to add in items like this:

itemDB.AddMultipleItems(new string[] { "Boots", "Gold Coin", "Apple" }, new int[] {1, 2, 3 });

Result: I get 3 Boots, 3 Gold coin and 3 Apple. when I should be getting 1 Boots, 2 Gold Coin & 3 Apples,

I've done a similar Method like this except it doesn't require array parameters and it works perfectly:

 public void AddItems(string itemKey, int amount)
{
    //First check if entered itemKey parameter exists in ItemCollection Dictionary
    if (ItemCollection.ContainsKey(itemKey))
    {
        Item item;
        item = ItemCollection[itemKey];
        if (item != null)
        {
            //check if we can add the item and the amount of it
            if (inventory.CanAddItem(item, amount))
            {
                //loop through the total amount
                for (int i = 0; i < amount; i++)
                {
                    //add the item
                    inventory.AddItem(item.GetCopy());
                }
                Debug.Log(itemKey + " Added Successfully!");
            }
            else
            {
                Debug.Log("Not enough space in Inventory");
            }
        }
        else
        {
            Debug.Log("Null Item");
        }
    }
    else
    {
        Debug.Log(itemKey + " does not exist in ItemDatabase's Dictionary");
    }
}

So basically another way of looking at it is how can I turn the AddItems(string itemKey, int amount) into AddItems(string[] itemKey, int[] amount). its the for loop, foreach loop and arrays that are kinda tripping me over since I'm not very good at those.

Appreciate any help, Thank you!


Solution

  • For every item you are iterating over the entire amount array which has 3 entries. I would actually expect that for each item you get 1+2+3 = 6 items added.

    Wouldn't you rather want to only get the amount to add from the one entry in amount with the same index as the given itemKey: amount[i]

    public void AddMultipleItems(string[] itemKey, int[] amount)
    {
        for (int i = 0; i < itemKey.Length; i++)
        {
            Item item;
            item = ItemCollection[itemKey[i]];
    
            if (inventory.CanAddItem(item, amount[i]))
            {
                inventory.AddItem(item.GetCopy());
            }
            else // your if condition here was redundant
            {
                Debug.Log("Inventory Full");
            }
        }
    }
    

    In general you already have a method for adding a single item so I wouldn't re-implement that for multiple ones but rather call it from the loop like

    public void AddMultipleItems(string[] itemKey, int[] amount)
    {
        for (int i = 0; i < itemKey.Length; i++)
        {
            // Instead of re-implement rather call your existing method
            // and treat each value pair as if ti was a single add call
            AddItems(itemKey[i], amount[i]);
        }
    }
    

    Then passing in two separate arrays is pretty error prone and hard to maintain. I would probably rather pass in a Dictionary<string, int> like e.g.

    public void AddMultipleItems(Dictionary<string, int> itemsToAdd)
    {
        foreach (var kvp in itemsToAdd)
        {
            AddItems(kvp.Key, kvp.Value);
        }
    }
    

    and then call it like

     itemDB.AddMultipleItems(new Dictionary<string, int>{ {"Boots", 1}, {"Gold Coin", 2}, {"Apple", 3}});