Looking for some help with theory on my crafting system

Hi Unity,

I am about to start creating my own crafting system and I am looking for some help on my theory before I jump in head first.

What I have today is a basic inventory system based around an Item script which defines what an item is and gives it things like an icon, tooltip, and other properties.

I have the ability to collect items in the game and add them to my inventory.

Now what I am looking to do is create a GUI window that allows you to put items in it and checks to see if you have certain items, if you have those items, in a gui box to the right a craftable item will show up.

So here is how I plan to implement this - and I am trying to find a better way to set up the recipe system:

Using OnMouseDown to detect if player has clicked on crafting table, if so GUI window pops up.

Use a for Loop to loop through current items in the players inventory.

(Now here is where I need some help to figure the best way to do this)

My original thought, which I think is probably terrible, is have a seperate set of variables in my crafting class, basically one variable for every item in the game.

As I loop through the player inventory, I can check if item.name == whateverItem. If it is, I increase a counter for that item.

Then after the for loop I can check the count for all my variables and if I have one of each of the items needed for itemX, then that icon shows up.

Something maybe like this?

private string basicWoodName = "basicWood"
private int basicWoodCount= 0;

private string basicStoneName = "basicStone"
private int basicStoneCount = 0;

for(int cnt; cnt < itemScript.playerInventory.count; cnt++)
{
  if(itemScript.playerInventory[cnt].name == basicWoodName)
  {
  basicWoodCount++;
  }

    if(itemScript.playerInventory[cnt].name == basicStoneName )
  {
  basicStoneCount++;
  }


}
//I would need one of these for every item.. ouch

//Then I would need recipe's - something like this maybe? (terrible I know)
if(basicWoodCount == 6 && basicStoneCount == 1)
{
  //Show GUI Icon for a Sword
}

//I would need one of these for every possible combination.. Ouch

I would like to just use a List for this somehow, but i cant figure out how I would access the invidivual items in the list to see if I have what I need.

Of course I will need to add a lot more to this script but I am just trying to formulate the basic idea of how this is going to work.

Thoughts on a better way to do this before i start?

Thanks!

Why loop through your inventory every time? You could just have a class like “CurrentItems” and just add and subtract from CurrentItems.wood–; as you pick em up and use them. And simply display something using those variables?

EDIT: (Your comments)

Hmm, I can’t think of a way to have em in some list without having to keep checking the list to see what the amounts are. But I guess it wouldnt take that long to check the inventory. But maybe use ENUMS instead of string to make it faster. Like:

enum Material {
    Stone,
    Wood,
    Glass,
    None
}

and in your Inventory (class)(your original) you could have

class Inventory {
   var material : Material;
   var ikon : Texture;
   var name : String;
   //etc whatever you have in this
}

and then (don’t know if you are using LIST) but if you aren’t:
var playerInventory : List. = new List.();

I ended up going with a different solution to solve this problem a bit more simple way. If anyone is interested in how this was done feel free to PM me.