- Home /
Lists within lists to make a crafting system
Hi,
So I created this class called recipe..All I am trying to do for now is to make a few lists with the different recipe's for items...
so for example..I want a list that contains the stick, rock and vine items and this list will be called 'axeRecipe' because these three things are needed to make an axe.
I have another list with the items my player currently has called 'items1'...so I want to basically go through these items and check if they are in any of the lists such as 'axeRecipe'....But right now I can't even get the lists to work. I am pretty inexperienced.
So how do I create a list containing the three items in the class below?
And then how can I make it so that the script checks my 'items1' list (list of items in players inventory) and goes through them to see if they are in the axeRecipe list?
public class recipe { public Item stick; public Item rock; public Item vine;
public recipe(Item item1, Item item2, Item item3)
{
Debug.Log ("hello");
}
}
Thanks
Philip
H$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$ This sounds very similar to a game I'm making right now :P
I am! You're a little cave man that has to run around and gather rocks/flint/sticks etc to craft stuff and stay alive during the night time. Eventual goal is find Woman and make Baby. :D
Answer by ShadyProductions · Jan 19, 2016 at 09:27 PM
You would have to setup if statements to check for the items individually, its not so effective to do it your way.
You would have an Inventory list which contains all the items and you would probably use a for loop to iterate through it, grab the necessary items, remove them and add an axe for example.
and set that up in some kind of method. Here's an example. Note: This is only an example, as I don't know how you have your inventory code set up.
void makeItem(int itemToMake, int item1, int item2, int item3) {
//add more if you want more items to be in the recipe
if (findItem(Inventory, item1) && findItem(Inventory, item2) && findItem(Inventory, item3)) {
//delete the items from list
//add new item to the list
}
}
bool findItem(List<int> list, int item) {
for (int i=0; i < list.Count; i++) {
if (list[i] == item) {
return true;
}
}
return false;
}
for (int i=0; i < list.Count; i++) { For some reason it won't recognize what a 'List' is here....? So the word 'List' is red and it won't understand list.Count...the'Count' is in red again... I have my using Systems.Collections.Generic up the top......any ideas? Thanks for this. It's eye-opening stuff for someone relatively new like myself.bool findItem(List list, int item) {
probably because its not List
but List<int>
but I wasn't 100% I just wrote this real quick as an example on a way to do things.
One other thing I don't understand is how I make sure that item1, item2 and item3 are a stick, a rock and a vine....?
In void Update() do I check if my inventory has these three items (using a similar loop found in findItem()) and then I can run void makeItem()?
Thanks for the help so far.
Well, you can't just make item1 = stick.. That's something that you need to remember on your own.
Like, in my inventory system I have 16 different items that are collectible. So, I set up two arrays that were both size 16. One was a String[] and the other was an int[]
So, when I pick up an item, I check the tag of the item I picked up in a for loop. Once the tag is == to the slot in the String[] I then set that slot # in the int[i] to +1. This isn't the most dynamic way to have an inventory system, but it works for my game and it's turned out pretty good. :D
Now that the int[] has been figured out all that's needed now is to set up your recipes.
I hope that helps you understand a little bit more of what's required of you.
I guess yeah or make a whole method for it that uses both methods lol, methodception
Your answer
Follow this Question
Related Questions
Trouble with Add-ing and Retrieving from a List of Custom Class 1 Answer
UNITYSCRIPT - Class's and Instances - Functions/Methods - Correct Usage 0 Answers
How to search a certain variable in a list of a created class (C#) 1 Answer
Trying to make a simple quest system, but i stuck how to do 0 Answers
Save an array with custom classes 1 Answer