mercredi 24 février 2021

check if clone is equal to original thing Unity

I'm working on a inventory system and i had working Crafting system, now i wanted to create RPG like crafting queue where you can click like 3 times on a item and it wil lcraft for you 3 time if you have enough resources, and i started working on it but for some reason, the original crafting system broke, here is what happens when you want to craft something When you click on Crafting Recipe:

//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:

//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 (this is where things are broken):

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

    }

}

so the problem is, in the InventoryItemList there are clones of one item, lets just say i have 2 irons and i need 1 iron to craft something, and my guess is that the problem is that i is clone so its not equal to the item and thats why it doesn't add 1 to itemCounter and then the script thinks we don't have enough resources to craft something, i tried to search and ask some of my friends how to check if that clone is equal to the item and i'm trying to fix this thing for like 2 days, i would love to hear any answers how to fix it or how to make my code more optimal, thanks for reading

Aucun commentaire:

Enregistrer un commentaire