微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检查克隆是否等于原始事物 Unity

如何解决检查克隆是否等于原始事物 Unity

我正在开发库存系统并且我有工作制作系统,现在我想创建类似于制作队列的角色扮演游戏,您可以在其中单击 3 次在一个项目上,如果您有足够的资源,它将为您制作 3 次,我开始研究它,但由于某种原因,原始的制作系统坏了,这是当你想要制作一些东西时会发生的事情 当您点击制作配方时:

//override Use Function
public override void Use() {

    //call AddCraftingItem from Inventory
    Inventory.instance.AddCraftingItem(this);

}

添加制作项目:

public void AddCraftingItem(CraftinRecipe newCraftingRecipe) {

    CraftingQueue.Enqueue(newCraftingRecipe);

    if(!isCrafting) {

        isCrafting = true;

        //start crafting
        StartCoroutine(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;

    }

}

工艺品:

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 中有一件物品的克隆,假设我有 2 个熨斗,我需要 1 个熨斗来制作一些东西,我猜问题是我是克隆的,所以它不相等这就是为什么它不向 itemCounter 添加 1 然后脚本认为我们没有足够的资源来制作某些东西,我尝试搜索并询问我的一些朋友如何检查该克隆是否等于项目,我正在尝试修复这个问题大约 2 天,我很想听到任何答案如何修复它或如何使我的代码更优化,感谢阅读

解决方法

而不是直接检查 i == itemItem 类中有一些属性,其中包含类似以下内容的项目类型信息

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

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。