检查克隆是否等于原始的东西Unity



我正在制作一个库存系统,我有一个工作的锻造系统,现在我想创造一个RPG,比如锻造队列,你可以点击3次,如果你有足够的资源,它会为你制造3次,我开始做这个,但出于某种原因,原来的锻造系统坏了,这是当你想要制造一些东西时发生的事情当你点击制作配方:

//override Use Function
public override void Use() {
//call AddCraftingItem from Inventory
Inventory.instance.AddCraftingItem(this);
}

AddCraftingItem:

public void AddCraftingItem(CraftinRecipe newCraftingRecipe) {
CraftingQueue.Enqueue(newCraftingRecipe);
if(!isCrafting) {
isCrafting = true;
//start crafting
StartCoroutine(CraftItem());
}
}

CraftItem:

private IEnumerator CraftItem() {
//check if queue is empty
if(CraftingQueue.Count == 0) {
Debug.Log("Queue is empty");
isCrafting = false;
yield break;
}
CraftinRecipe currentRecipe = CraftingQueue.Dequeue();
//check if we have enough resources
//this is where things broke
//CraftItem return false
if(!currentRecipe.CraftItem()) {
//Debug.Log("You don't have enough Resources");
CraftingQueue.Clear();
isCrafting = false;
yield break;
}
Debug.Log("TEST");
yield return new WaitForSeconds(currentRecipe.craftTime * 1.1f);
//add item to inventory
AddItem(currentRecipe.result);
Debug.Log("Added " + currentRecipe.result + " to inventory");
//check if continue crafting
if(CraftingQueue.Count > 0) {
yield return StartCoroutine(CraftItem());
} else {
isCrafting = false;
}
}

CraftItem:

public bool CraftItem() {
if(!CanCraft()) {
//Debug.Log("CanCraft returned false");
return false;
} else {
Debug.Log("CanCraft returned true");
}
RemoveIngredientsFromInventory();
//start crafting
ParentCraftingSlot.StartCrafting();
return true;
}

CanCraft功能:

//function that return bool if we can craft the Item
public bool CanCraft() {
//loop trough each ingredient type of ingredient in ingredient
//(don't worry bro i don't understand what i just said too)
foreach(Ingredient ingredient in ingredients) {
//bool if we Contains current Ingredients
//here this function return false
bool ContainsCurrentIngredient = Inventory.instance.ContainsItem(ingredient.item, 
ingredient.amount);
//if ContainsCurrentIngredient is false
if(!ContainsCurrentIngredient) {
//we return false
return false;
}
}
//return true
return true;
}

ContainItems(这是东西被打破的地方):

//function that return true or false
//if we have enough Item to craft something
public bool ContainsItem(Item item, int amount) {
//make some variables objects etc
int ItemCounter = 0;
//loop through InventoryItemList with variable i type of Item
//(don't worry i don't know what i just said too #6)
foreach(Item i in InventoryItemList) {
//if i is equal to item
//this is the broken part
if(i == item) {
Debug.Log(i);
Debug.Log(item);
//we add 1 to Item Counter
ItemCounter++;
} else {
Debug.Log("i is not equal to item");
}
}
//if ItemCounter is bigger then or equal to amount
if(ItemCounter >= amount) {
/*Debug.Log("ContainsItem returned true");
Debug.Log(ItemCounter);*/
//we return true
//that means we have enough items to craft something
return true;
} else /*else*/ {
/*Debug.Log("ContainsItem returned false");
Debug.Log(ItemCounter);*/
//we return false
//that means we don't have enough item to craft something
return false;
}
}

问题是,InventoryItemList有克隆的一个项目,我们只说我有两个熨斗,我需要1铁工艺,和我的猜测是,问题是,我是克隆不等于项目,这就是为什么它不加1 itemCounter然后脚本认为我们没有足够的资源来工艺,我试着搜索,问我的一些朋友如何检查如果克隆等于项目和我试图修复这个东西像2天,我很乐意听到任何答案如何修复它或如何使我的代码更优化,感谢阅读

与其直接检查i == item,不如在Item类中设置一些包含项目类型信息的属性,如

public class item
{
public enum Type
{
IronBar, GoldBar //etc
}
public Type itemType;
}

ContainItems()中你可以使用

public bool ContainsItem(Item.Type itemType, int amount){
// other code
foreach(Item i in InventoryItemList) {

if(i.itemType == itemType) {

ItemCounter++;

}
// other code
}

最新更新