Hi,
I am currently working on a crafting system using scriptable objects for items and I am trying to compare two lists to see if the items I have in the crafting slots are equal to any of the recipes in another list.
At the moment in the CheckForRecipe() method (i’m not that great with coding) but im assuming it is checkng both lists to see if ANY of the items are the same? But I want to compare and see if ALL the items in ItemList are the same.
Hope this makes sense and TIA!
I am not very good with explaining myself so hopefully the diagram below will help lol.
Crafting Manager script:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace Deano
{
public class CraftingManager : MonoBehaviour
{
[SerializeField] PlayerInventory playerInventory;
public List<Item> itemList;
public Recipe[] recipes;
public CraftingSlot resultSlot;
public CraftingSlot[] craftingSlots;
void Update()
{
CheckForSlotItem();
CheckForRecipe();
}
void CheckForSlotItem()
{
foreach (CraftingSlot slot in craftingSlots)
{
//If a crafting slot has a child i.e... has an item in the slot
if (slot.transform.childCount > 0)
{
//Set the slots item to the item in the slot
slot.item = slot.gameObject.transform.GetChild(0).GetComponent<InventoryItem>().Item;
slot.itemInSlot = true;
}
else
{
itemList.Remove(slot.item);
slot.itemAddedToList = false;
slot.itemInSlot = false;
slot.item = null;
}
if (slot.itemInSlot == true)
{
if (slot.itemAddedToList == false)
{
itemList.Add(slot.item);
slot.itemAddedToList = true;
}
}
else
{
slot.itemInSlot = false;
slot.itemAddedToList = false;
itemList.Remove(slot.item);
}
}
}
void CheckForRecipe()
{
resultSlot.item = null;
Debug.Log("Checking for recipe");
for (int i = 0; i < recipes.Length; i++)
{
Debug.Log("Looping through recipes");
if (itemList.Any(x => recipes[i].materials.Any(y => y == x)))
{
Debug.Log("Can craft item");
}
else
{
Debug.Log("Cannot craft item");
}
}
}
}
}
Recipe SO script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Deano
{
[CreateAssetMenu(fileName = "Recipe", menuName = "ScriptableObjects/Crafting Recipe")]
public class Recipe : ScriptableObject
{
public List<Item> materials;
public Item craftedItem;
public int craftedAmount;
}
}