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!
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;*
}