Inventory ch 4 Richard Gesick Objectives Creating a
Inventory ch 4 Richard Gesick
Objectives Creating a storage system for Game. Objects 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 • • 1 -2
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. 1 -3
Item count modifications • Buying, selling, and trading items • Dropping and picking up items • Destroying or using items 1 -4
Key. Value. Pair • Uses parallel lists which is not normally a good idea but is probably the easiest way to handle this List<Key. Value. Pair<int, string>> items = new List<Key. Value. Pair<int, string>>(); List<Key. Value. Pair<int, int>> item. Count = new List<Key. Value. Pair<int, int>>(); . . . items. Add(new Key. Value. Pair<int, string>(i, inv. Items[i])); item. Count. Add(new Key. Value. Pair<int, int>(i, 0)); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -5
Displaying the inventory Inventory. Gui() GUILayout. Begin. Area(new Rect(0, 50, 400)); GUILayout. Begin. Horizontal(); if (GUILayout. Button(item. Count[0]. Value. To. String() + " " + inv. Items[0]. name, GUILayout. Height(75))) { inv. Items[0]. Set. Active(true); Remove. From. Inventory(1, inv. Items[0]); } if (GUILayout. Button(item. Count[1]. Value. To. String() + " " + inv. Items[1]. name, GUILayout. Height(75))) { inv. Items[1]. Set. Active(true); Remove. From. Inventory(1, inv. Items[1]); } GUILayout. Button(item. Count[2]. Value. To. String() + " " + inv. Items[2]. name, GUILayout. Height(75)); GUILayout. End. Horizontal(); 1 -6
Gui Trigger • Create a boolean controlled by a key if (Input. Get. Key. Down(Key. Code. I)) { show. Inventory = (show. Inventory) ? false : true; } And use the Boolean in On. Gui() Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -7
Add to inventory • If the item already exists, add to the count of the item int val = item. Count[i]. Value + How. Many; item. Count[i] = new Key. Value. Pair<int, int>(item. Count[i]. Key, val); • Else add it as a new item int val = item. Count[i]. Value + How. Many; inv. Items[i] = New. Item; items[i] = new Key. Value. Pair<int, Game. Object>(i, New. Item); item. Count[i] = new Key. Value. Pair<int, int>(i, val); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 -8
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 1 -9
- Slides: 9