- Home /
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!
This question isn't something we can answer for you in 500 words or so. To give you a good response would require a conversation. Unity Answers isn't for conversations - please move this to the forums ins$$anonymous$$d, where you're much more likely to get good feedback.
Try these tutorials. They are extremely long, but he goes into detail on how to set up an item/attribute system.
http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial
Ya I have actually based some of my current system off of Petey's. I have a good working system going now - might not be the most efficient but it works well.
Thanks for the link though!
Answer by madmike6537 · Feb 09, 2013 at 05:01 AM
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.
Answer by Vonni · Feb 01, 2013 at 05:31 PM
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.();
Hmm so basically when I add the items in my inventory also add them to another class called currentItems and check there for what I have? $$anonymous$$akes sense.
What about my variable method though, I assume I would still have to use that but just put it in another class. Can you think of a way to use a list for that ins$$anonymous$$d of a variable for each item's name and count?
I am going to leave this open for just a little bit and see if I can get some other ideas as well. Thanks very much!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Problem with dropping items from my inventory 0 Answers
Problem at crafting 1 Answer
Can someone help me with an inventory? 1 Answer
List array in GUI.Box 0 Answers