- Home /
Inventory system?
I need help creating an inventory system much like Minecraft's (minus the crafting). I have somewhat of an idea of what I have to do, but all the tutorials I look at or articles I read that explain how to make an inventory system don't seem to fully explain everything. It has really been bugging me...
static public Dictionary<> inventory; // For items in inventory
public class Item
{
//For Item creation
}
I need some explanation, and I need to know how to make it work! Answers Appreciated.
I happen to be working on something similar to that myself, and you need a way to display the content of the inventory. The way I display the inventory is by creating a 2-dimensional array that holds the item data and looping through each part of the array creating a button for each iteration of the 2D array, and displaying the name on the button. The reason it is 2D is because there is a width and a height of the grid of the buttons. You should learn the Unity GUI stuff.
I haven't finished yet, so I still need to figure out how to drag the buttons and drop the items and use them and whatever.
You can use a variable to hold the class data and use the buttons if statement and loop index to check if the value is or isn't null to place/pick up the GUI item (swap between null and that item), if that variable isn't null draw some GUI element at the mouse position to show that you're holding it. I'm not sure how you'd make it drag and drop though.
Yes, I was thinking that if you're holding the button, then you can take the position of the mouse on the screen and draw the button at a point where the mouse pointer is at the middle of the button by finding the exact middle of the button and working backwards from there to set the position of the button so the middle is right where the mouse is.
Here is a great tutorial, that $$anonymous$$ches you how to create an inventory system from scratch, with the most common operations: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ
Answer by Negagames · Jan 20, 2014 at 11:05 PM
You have a start, most of what you need done is done with that snippet actually, from here on all you have to do is define item properties (ex. Name, Type ect...) and create some UI (user interface )handeling
If you're not already familiar with it, Unity has a great API where you can learn to use all of the different functions provided w/ the unity engine. Here is a link the the API on the subject of GUI: http://docs.unity3d.com/Documentation/ScriptReference/GUI.html
It depends on how you want your inventory to work but the concept is simple. If you have one array for the 'bag' of items, you can have another for equipped/hotkeyed ones like you would find in $$anonymous$$inecraft. If you want single slots then just have single variables of the item class. As @Negagames hinted above, loop through those arrays/read variables for the GUI. You can give the class a texture variable for the GUI to read.
Adding/removing items is just adding/removing an element to the array, a gameobject can have a script with the item variables for the new element you're adding. If you make it 2 dimensional then you can pretty easily use 2 for loops to make a grid of buttons and use the current index to select/grab/etc. an item from the array.
'Using' them is similar to the GUI, read whatever item you need to via hotkey or whatever, and based on the variables in that class call certain functions that can use other class properties, like food and how much health it gives you.
I think have something. I just need to finish it, I'll work on it tomorrow.
Answer by YoungDeveloper · Jan 20, 2014 at 11:28 PM
Before writing an actual code, i would like talk about couple things, like what is overall basic idea from game creation perspective itself.
Map designers, quest creators and many others, these people mostly never code when creating maps, quests, place npc etc. They have ready custom built tools for doing that, all what they do is move sliders, search through lists and place/write (not code).
So, before actually creating the game experience itself, we need to make sure that we the right tools for doing that process, it will be taking a long time to create the map, so each helpful thing for the map designers can save tens of hours.
One way would be storing ready prefabs in one folder, but imagine if prefab amount is around 4 thousand, considering all little props, items, mobs etc. Artist will drive nuts working like that. So, custom editors is the only option, you could sort specific item lists under different categories, also show how they look or will look. That would be much more easier to work with and focus on map building than trying to find specific item for 10 minutes.
It means, each item, Quest, NPC, monster, gun, attachment, etc should be somehow recognizable (specific number or something), but how? If in simple case all what it has is a tag ?
Creating a structured list for each type of item would be good logic, for example one list for pistols, one for magazines ans so on. When the list is created you can easily access it from any point in your game and custom editors. Which means if you update you food list, your custom editor will automatically will be updated.
So basically when having these lists, you can spawn anything and anywhere And track things via Log.
So first, we create the list (array) which will hold information about the items. This script should be on player itself.
public class Items : MonoBehaviour{
//We create a Node, possible information each this current item will hold
[System.Serializable] //Without this next class "Item" wouldn't not show in inspector
public class Item{
public string name; // its name
public int weight; //just showing the idea
}
public Item[] allItems;
//This is just for showing how it might work
public InfoAboutItem(int index){ //This will be called from player
Debug.Log("You picked up " + allItems[index].name + " and it weights " + allItems[index].weight);
}
}
After attaching script you basically now can create item list in any size you want, and each can contain lots of information.
Now we need a script which will be located on item itself, so we would know what item is that.
public class ThisItem : MonoBehaviour{
public int itemIndex; // remember that first element is 0 not 1
}
Yes, that's it :-).
Now the script which will find the item. Also on player.
public class Player : MonoBehaviour {
void OnTriggerEnter ( Collider col ){
if (col.tag == "Item") { //All previous items in game can be under one tag "Item"
GetComponent<Items>().InfoAboutItem( col.GetComponent<ThisItem>().itemIndex );
Destroy(col.gameObject);
}
}
}
Set the list in size of 1 (in inspector), in first element fields, enter its name and weight.
Place ThisItem script on the pickable item, set index to 0, tag should be "Item"
After triggering the item the text will appear and it will be destroyed.
This is the basic idea how you can create very advanced item in game structures.
A bit of a lecture, but i hope it was interesting to ones just starting :).
$$anonymous$$y game is in 2d and I converted it so that it would work within my game, though however it didn't work. I do however understand it better. Things like this have always been a hassle for me. I try to play around with it more, if I can't come up with anything, I'll just get something from the asset store to help.
Answer by darkal · Jan 21, 2014 at 01:41 AM
i recommend true/false statements such as if something is in slot 1 the slot is false so it check slot 2 i like to use a count to keep track of things that like lets the script check itself