C# 向数组中添加对象项会导致错误结果

C# 向数组中添加对象项会导致错误结果,c#,visual-studio,debugging,C#,Visual Studio,Debugging,目前正在一个基本RPG系统上工作,我遇到了一个方法错误。该方法用于将项目(代码中的类)和可选金额添加到玩家库存(项目[]数组) 这是项类的代码: public class Item { public string Name; public int quantity; public int maxstack; public int[] stats = new int[5]; public Item(string name, int Amount = 1

目前正在一个基本RPG系统上工作,我遇到了一个方法错误。该方法用于将项目(代码中的类)和可选金额添加到玩家库存(项目[]数组)

这是类的代码:

public class Item

{
    public string Name;
    public int quantity;
    public int maxstack;

    public int[] stats = new int[5];

    public Item(string name, int Amount = 1, int MaxStack = 10, int ATK = 0, 
    int DEF = 0, int MAT = 0, int MDF = 0, int SPD = 0)
    {
        Name = name;
        quantity = Amount;
        maxstack = MaxStack;
        stats[0] = ATK;
        stats[1] = DEF;
        stats[2] = MAT;
        stats[3] = MDF;
        stats[4] = SPD;
    }
}
关键变量是“数量”和“maxstack”

现在,当向玩家库存添加一个物品时,问题就来了,关于这些变量。 该方法在向库存添加项目时,使用Array.IndexOf()搜索库存中的空槽和同一项目的任何堆栈

这是AddItem()方法的代码:

public void AddItem(Item item, int Amount = 1)
{
    for (int i = Amount; i > 0; i--)
    {
        int ItemIndex = Array.IndexOf(inv, item); // Searches for a matching item
        int EmptySlot = Array.IndexOf(inv, null); // Searches for an empty slot

        ItemCheck:
        if (ItemIndex != -1) // ItemIndex will equal -1 if no matching item was found
        {
            if (inv[ItemIndex].quantity >= inv[ItemIndex].maxstack) // Is the quantity/stack of the found item equal to its maximum stackable value?
            {
                ItemIndex = Array.IndexOf(inv, item, ItemIndex + 1); // If yes, search for another index.
                goto ItemCheck;
            } else {
            inv[ItemIndex].quantity++; // If the stack hasn't reached its max, increase it by one.
            }
        }
        else
        {
            inv[EmptySlot] = item; // If no matching item was found, use an empty slot to create a new stack.
            inv[EmptySlot].quantity = 1;
        }
    }
}
现在,假设我创建了一个名为“stick”的项目,它最多只能堆叠3个。当运行AddItem(stick,3)
并列出每个堆栈的数量时,控制台返回

Stick x1
Stick x1
有人能帮我吗?为什么我的代码将堆栈转换回1的数量

编辑:

添加1、2或3根棍棒将返回正确的数量,但只有在堆栈达到其最大值时添加更多棍棒才会抛出错误的结果

编辑:


添加6根棍子将返回2个堆叠,每个堆叠中包含3个项目。添加7根棍子会返回3个堆叠,每个堆叠中有1个物品。

这里最大的问题是您使用物品来存储数量。。。但该物品在您的库存中的多个位置使用。因此,当您将数量更改回1时,您正在为库存中存在该项目的每个插槽更改数量

也就是说,您没有该项目的副本,您多次拥有同一对象

有很多方法可以解决这个问题,但也许应该创建一个名为InventorySlot的新类,并将数量放入其中

public class InventorySlot
{
   public Item Item {get; set;}
   public int Quantity {get; set;}
}
现在你的玩家清单是一个清单插槽数组。。。像这样 考虑到你的球员身上有这样的东西

 public InventorySlot[] inv = new InventorySlot[5];
然后

public void AddItem(Item item, int amount = 1)
{
    var slot = inv.FirstOrDefault(s => s?.Item == item && s.Quantity < item.maxstack);
    if (slot != null)
    {
        slot.Quantity++;
    }
    else
    {
        slot = inv.FirstOrDefault(s => s == null);
        if (slot != null)
        {
            slot.Item = item;
            slot.Quantity = 1;
        }
    }
}
public void AddItem(项目,整数金额=1)
{
var slot=inv.FirstOrDefault(s=>s?.Item==Item&s.QUOTEs==null);
如果(插槽!=null)
{
slot.Item=项目;
槽数量=1;
}
}
}

这里最大的问题是您使用项目存储数量。。。但该物品在您的库存中的多个位置使用。因此,当您将数量更改回1时,您正在为库存中存在该项目的每个插槽更改数量

也就是说,您没有该项目的副本,您多次拥有同一对象

有很多方法可以解决这个问题,但也许应该创建一个名为InventorySlot的新类,并将数量放入其中

public class InventorySlot
{
   public Item Item {get; set;}
   public int Quantity {get; set;}
}
现在你的玩家清单是一个清单插槽数组。。。像这样 考虑到你的球员身上有这样的东西

 public InventorySlot[] inv = new InventorySlot[5];
然后

public void AddItem(Item item, int amount = 1)
{
    var slot = inv.FirstOrDefault(s => s?.Item == item && s.Quantity < item.maxstack);
    if (slot != null)
    {
        slot.Quantity++;
    }
    else
    {
        slot = inv.FirstOrDefault(s => s == null);
        if (slot != null)
        {
            slot.Item = item;
            slot.Quantity = 1;
        }
    }
}
public void AddItem(项目,整数金额=1)
{
var slot=inv.FirstOrDefault(s=>s?.Item==Item&s.QUOTEs==null);
如果(插槽!=null)
{
slot.Item=项目;
槽数量=1;
}
}
}

我不明白您为什么要尝试将项目添加到数组中,因为它是一个列表
您可以简单地使用
列表
,它提供了诸如
添加
删除
删除
插入
索引等方法
如果你想得到一个数组,你可以调用
yourList.ToArray()

  • 是使用一些值初始化
    列表的示例
    
  • 列表中删除项目的示例
看看这个例子:

List<Item> yourList = new List<Item>(); //Create an empty list
/* Or
 * List<Item> youtList = new List<Item>()
 * {
 *     new Item(),
 *     new Item() //etc
 * }
 */
yourList.Add(new Item()); //Add an item 
yourList.Insert(0, new Item()); //Insert an item to the 0 index
yourList.Remove(yourItem); //Remove an item directly
yourList.RemoveAt(0); //Remove an item by its index in the list
Item[] yourArray = yourList.ToArray(); //Convert the list List<T> to an array T[]
List yourList=新列表()//创建一个空列表
/*或
*List youtList=新列表()
* {
*新项(),
*新项()//等
* }
*/
添加(新项())//添加项目
插入(0,新项())//在0索引中插入一项
您的列表。删除(您的项目)//直接删除项目
yourList.RemoveAt(0)//通过列表中的索引删除项目
Item[]yourArray=yourList.ToArray()//将列表转换为数组T[]
看看这本书是否有任何疑问


如果出于任何特定原因,您需要使用数组,请告诉我,我将编辑此答案。

我不明白您为什么要尝试将项目添加到数组中,因为它是一个列表
您可以简单地使用
列表
,它提供了诸如
添加
删除
删除
插入
索引等方法
如果你想得到一个数组,你可以调用
yourList.ToArray()

  • 是使用一些值初始化
    列表的示例
    
  • 列表中删除项目的示例
看看这个例子:

List<Item> yourList = new List<Item>(); //Create an empty list
/* Or
 * List<Item> youtList = new List<Item>()
 * {
 *     new Item(),
 *     new Item() //etc
 * }
 */
yourList.Add(new Item()); //Add an item 
yourList.Insert(0, new Item()); //Insert an item to the 0 index
yourList.Remove(yourItem); //Remove an item directly
yourList.RemoveAt(0); //Remove an item by its index in the list
Item[] yourArray = yourList.ToArray(); //Convert the list List<T> to an array T[]
List yourList=新列表()//创建一个空列表
/*或
*List youtList=新列表()
* {
*新项(),
*新项()//等
* }
*/
添加(新项())//添加项目
插入(0,新项())//在0索引中插入一项
您的列表。删除(您的项目)//直接删除项目
yourList.RemoveAt(0)//通过列表中的索引删除项目
Item[]yourArray=yourList.ToArray()//将列表转换为数组T[]
看看这本书是否有任何疑问


如果出于任何特定原因,您需要使用阵列,请告诉我,我将编辑此答案。

,inv是什么?我想猜测一下,它可能不起作用,因为
数组。IndexOf
在数组中找不到与传递给函数的
对象的值相等的项。凝视
转到
。。。。。。你知道那些人