【发布时间】:2019-03-02 03:44:51
【问题描述】:
我一直在尝试创建库存系统,但遇到了问题。我有一个名为“项目”的列表,这里是管理要添加到库存中的项目的函数的代码。
我在使用此代码时遇到的错误是,每当我尝试将不存在的物品添加到库存中时,它都能正常工作,但是每当我尝试在库存中再次添加相同的物品时,无论我写的数量如何,它都会增加stackSize 减 1 并且不再添加。我也尝试过使用数组,但它仍然无法正常工作,该项目还发生了一些其他奇怪的事情,但我想先解决这个问题。谢谢!
public void AddToInventory(Item _item, int amount = 1)
{
foreach(Item item in items)
{
if(item.Name == _item.Name)
{
item.stackSize += amount;
FlushList();
return;
}
}
Item item2 = new Item();
item2 = _item;
item2.stackSize = amount;
items.Add(item2);
}
这是我测试它的方式:
private void Awake()
{
items = new List<Item>();
itemsInstantiated = new List<GameObject>();
AddToInventory(itemDb.GetItem("Material"));
AddToInventory(itemDb.GetItem("Material"));
AddToInventory(itemDb.GetItem("Material"));
AddToInventory(itemDb.GetItem("Material"));
// Stacksize of materials after this code is 2.
// Doesn't matter how many times i call the function above.
AddToInventory(itemDb.GetItem("Water Bottle(Full)"), 21);
AddToInventory(itemDb.GetItem("Apple"));
FlushList();
}
FlushList() 方法:
public void FlushList()
{
// Visual stuff.
// There is a more efficent way but it's just a project to pass time so
// didn't bother.
foreach(GameObject go in itemsInstantiated)
{
Destroy(go);
}
foreach(Item item in items)
{
GameObject inst = Instantiate(ItemPrefab, ListView.transform);
itemsInstantiated.Add(inst);
inst.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = item.Name;
inst.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = item.Description;
inst.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text = item.stackSize.ToString();
if(item.isConsumeable)
{
inst.transform.GetChild(3).gameObject.SetActive(true);
inst.transform.GetChild(3).GetComponent<Button>().onClick.AddListener(() => { ConsumeItem(item); });
}
}
}
ConsumeItem() 方法:
public void ConsumeItem(Item item)
{
item.consumeAction?.Invoke();
PlayerNeeds pn = GetComponent<PlayerNeeds>();
// Increase methods don't touch the list.
pn.IncreaseHunger(item.hungerIncrease);
pn.IncreaseThirst(item.thirstIncrease);
pn.IncreaseSleepiness(item.thirstIncrease);
RemoveItem(item);
}
物品数据库: 项目数据库是我存储所有项目的地方。那里的项目只有名称和描述。
public Item GetItem(string itemName, int amount = 1)
{
// Amount here is used by other functions such as crafting recipes.
// I feel like this is where the problem is.
foreach(Item item in AllItems)
{
if(item.Name == itemName)
{
Item newItem = item;
newItem.stackSize = amount;
return newItem;
}
}
print("Item: " + itemName + " could not be found!");
return null;
}
物品类别:
public class Item
{
public string Name;
public string Description;
public int categoryIndex = 1;
public bool isConsumeable;
public int stackSize = 1;
public int hungerIncrease;
public int thirstIncrease;
public int sleepIncrease;
public Action consumeAction;
}
_item 是物品数据库中的物品,它的堆栈大小始终为 1,我要求输入“物品”,因为如果玩家的库存中不存在该物品,我将添加该物品。
一些例子:
* AddToInventory(item A, 3)
结果:我的库存中有 3 件 A 件(效果很好)。
*AddToInventory(item A, 3)AddToInventory(item A, 3)
结果:4 项 A。
**AddToInventory(item A, 45)AddToInventory(item A, 57)
结果:46 项 A。
【问题讨论】:
-
您可能需要添加
_item.stackSize而不是amount。而你的Item item2 = new Item();什么也没做…… -
_item只是项目数据库中的一个项目,它的堆栈大小始终为 1。我也不明白为什么item2没有做任何事情。 -
当您发现该项目已在列表中时,您
return无需再次将其添加到列表中。 -
@JohnnyMopp,为什么我们需要再次添加它?它已经是列表的一部分..
-
@JohnnyMopp 他没有尝试向列表中添加第二个重复项目,他正在更新列表中的项目以显示库存中有多少