Inventory ch 4 Richard Gesick
Objectives Creating a storage system for GameObjects Adding items to the inventory Removing items from the inventory Initializing the inventory Setting the inventory size to be dynamic Making it possible to have multiples of items Setting quick items that can be used by our custom quick item inputs Accessing the inventory using our custom inventory inputs Displaying the inventory on screen via GUI
Features of an inventory Limits - weight and slot size or both combined Accessibility - menu system, quick-items, and an item bar Order - the physical size, item type, slot size, or alphabetical order. Physical size can be either lightest to heaviest or heaviest to lightest. By item type, healing items should be kept separate from weapons and armor. These separate item types can be split up into multiple submenus: one each for weapons, armor, healing items, etc.
Item count modifications Buying, selling, and trading items Dropping and picking up items Destroying or using items
KeyValuePair Uses parallel lists which is not normally a good idea but is probably the easiest way to handle this List<KeyValuePair<int, string>> items = new List<KeyValuePair<int, string>>(); List<KeyValuePair<int, int>> itemCount = new List<KeyValuePair<int, int>>(); . . . items.Add(new KeyValuePair<int, string>(i, invItems[i])); itemCount.Add(new KeyValuePair<int, int>(i, 0)); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Displaying the inventory InventoryGui() GUILayout.BeginArea(new Rect(0, 50, 400, 400)); GUILayout.BeginHorizontal(); if (GUILayout.Button(itemCount[0].Value.ToString() + " " + invItems[0].name, GUILayout.Height(75))) { invItems[0].SetActive(true); RemoveFromInventory(1, invItems[0]); } if (GUILayout.Button(itemCount[1].Value.ToString() + " " + invItems[1].name, GUILayout.Height(75))) invItems[1].SetActive(true); RemoveFromInventory(1, invItems[1]); GUILayout.Button(itemCount[2].Value.ToString() + " " + invItems[2].name, GUILayout.Height(75)); GUILayout.EndHorizontal();
Gui Trigger Create a boolean controlled by a key if (Input.GetKeyDown(KeyCode.I)) { showInventory = (showInventory) ? false : true; } And use the Boolean in OnGui() Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Add to inventory If the item already exists, add to the count of the item int val = itemCount[i].Value + HowMany; itemCount[i] = new KeyValuePair<int, int>(itemCount[i].Key, val); Else add it as a new item invItems[i] = NewItem; items[i] = new KeyValuePair<int, GameObject>(i, NewItem); itemCount[i] = new KeyValuePair<int, int>(i, val); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using/removing from inventory If the item exists, subtract 1/how many from inventory as long as enough exist Once the item is empty, shuffle the other elements in the collection up If you are using parallel collections with keyvalue pairs, you have to remember to shift corresponding elements in both collections Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley