【问题标题】:Unity - Adding multiple array parameters in method (for adding multiple items)Unity - 在方法中添加多个数组参数(用于添加多个项目)
【发布时间】:2020-10-26 07:39:56
【问题描述】:

我正在尝试实现一种可以调用来创建多个项目的方法

这是我试图让它工作的方法

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;
            }

        }
    }

}

然后会调用这个方法来添加这样的项目:

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

结果:我得到 3 个靴子、3 个金币和 3 个苹果。当我应该得到 1 个靴子、2 个金币和 3 个苹果时,

我也做过类似的方法,除了它不需要数组参数而且效果很好:

 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");
    }
}

所以基本上另一种看待它的方式是我如何将 AddItems(string itemKey, int amount) 变成 AddItems(string[] itemKey, int[] amount)强>。它的 for 循环、foreach 循环和数组有点让我绊倒,因为我不太擅长这些。

感谢任何帮助,谢谢!

【问题讨论】:

    标签: arrays for-loop unity3d foreach inventory


    【解决方案1】:

    对于每个item,您都在迭代整个amount 数组,该数组有3 个条目。我实际上希望为每个项目添加 1+2+3 = 6 项目。

    难道您不想只从amount 中的one 条目中获取要添加的金额,其索引与给定的 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");
            }
        }
    }
    

    一般来说,您已经有了添加单个项目的方法,所以我不会为多个项目重新实现该方法,而是像从循环中调用它

    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]);
        }
    }
    

    然后传入两个单独的数组很容易出错并且难以维护。我可能宁愿传递一个Dictionary&lt;string, int&gt;,例如

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

    然后像这样称呼它

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

    【讨论】:

    • 你说得对,最好将我的 Additems() 方法重复用于 1 个项目。我之前确实考虑过重用它,但只是不知道 for 循环等如何工作。每当我处理循环时,感觉就像我的大脑在迷宫中哈哈。无论如何,感谢您对重用方法的帮助。
    • Whenever I deal with loops, it feels like my brain is in a maze lol ^^ 这更有理由尽可能将其拆分为单独的方法;)从最小(原子)解决方案开始总是好的做法(就像这里添加单个项目)然后如果这样做会增加复杂性,但会重新使用您的原子解决方案。这样你可以例如还可以扩展或更改添加单个项目的方式,而无需完全触及多个方法,因为您只需确保单个添加有效
    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多