List and crafting structure and logic.

I have been asking for a solution and trying to find one for the last few days. The problem is with my code for my crafting system. What Im trying to do is, press a craft button, it uses the craft void with the recipe it wants to make, the inventory list is checked if it has the items needed(here is the problem, I do not know how to structure it, it needs to not every duplicate the same item in the list), and then if it receives the items, it contineus with the craft void, and returns the recipes output item. P.S I made a system to check for items, but it still does not help, and second, any example with my code would be much appreciated, also please follow up on the question! :slight_smile:

Code(Inventory Script):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerInventory : MonoBehaviour {

	public int MaxInventory = 26;

	public List<Item> InventoryItems = new List<Item>();


	void Update(){
	}

	public void AddItem(Item _item){

		if (InventoryItems.Count < MaxInventory) {
			InventoryItems.Add(_item);
		}
	}

	public void RemoveItem(Item _item){

		InventoryItems.Remove (_item);
	}

	public void SaveItems(){

		for (int i = 0; i<MaxInventory; i++) {
			PlayerPrefs.SetString("Itemn"+i,InventoryItems*.ItemName);	*

_ PlayerPrefs.SetString(“Itemt”+i,InventoryItems*.ItemType);_
_ PlayerPrefs.SetInt(“Itemv”+i,InventoryItems.ItemValue);
PlayerPrefs.SetInt(“Itemd”+i,InventoryItems.ItemDur);
}
LoadItems ();
}
public void LoadItems(){
InventoryItems.Clear();
for (int i = 0; i<MaxInventory; i++) {
Item it = new Item();
it.ItemName = PlayerPrefs.GetString(“Itemn”+i);
it.ItemType = PlayerPrefs.GetString(“Itemt”+i);
it.ItemValue = PlayerPrefs.GetInt(“Itemv”+i);
it.ItemDur = PlayerPrefs.GetInt(“Itemd”+i);
if(it.ItemName.Length <= 2 && it.ItemType.Length <= 2){
}
else{
InventoryItems.Add(it);
}
}
}*_

* public void Craft(CraftingItem Recipe){*
* Recipe.InputItem.Sort ();*
* List _items = new List ();*

* //Find out the number and use CheckForItemsAndReturn();*

* if (Recipe.InputItem == items) {
foreach(Item it in items){
InventoryItems.Remove(it);*
* }*

* InventoryItems.Add(Recipe.OutputItem);*

* }*
* else{*
* Debug.Log(“Invalid Items.”);*
* }*
* }*

* //For Multiple*
* public List CheckForItemsAndReturn(Item checkfor, int count){*
* List il = new List();*

* foreach(Item it in InventoryItems){*
* if(it == checkfor){*
* if(il.Count <= count){*
* il.Add(it);*
* }*
* }*
* }*

* if (il.Count == count) {*
* return il;*
* }*
* else{*
* return null;*
* }*
* }*

}
Code(Item and Crafting Recipe classes):
using UnityEngine;
using System.Collections;

public class Item
{
* public string ItemName;*
* public string ItemType;*
* public int ItemValue;*
* public int ItemDur;*
}

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CraftingItem
{
* public Item OutputItem;*
* public List InputItem;*
}

What about…

Add this logic in somewhere you think it fits:

public static bool ContainsAllItems(List<T> a, List<T> b)
{
    return !b.Except(a).Any();
}

So then, you can check if your INVENTORY contains all items that your RECIPE requires.
Applying that function that I wrote above, you would use something like…

bool craftable = ContainsAllItems(inventory, inputItems);

And please, stop writing variables starting with an uppercase letter :frowning: It is ugly (and wrong).

When coding in C#, you should follow its patterns, ahm, C# has properties, properties are ~functions~ that replace trashy getters and setters, these properties are written with an uppercase letter.
So, if you have an variable (or attribute) called name, for example, you just write it “lowercased”. And, if you think it is fine to add a property to it, you will write some stuff like:

public string Name
{
    get { return name; }
    set { name = value; }
}

That’s it.