arrays for inventory
BlitzMax Forums/BlitzMax Programming/arrays for inventory
| ||
Hey guys, how would I go about using a 2d array to hold inventory data? Never really worked with arrays much. But I don't think I can get away from them for an inventory system. Something like inventory[3,3] inventory[0,0] = 'Sword' inventory[0,1] = 'Quantity' inventory[1,0] = Shield inventory[1,1] = quantity Tell me if I understand the above correct. For example 0,1 is merely an index pointing to a location, so the 0,1 number doesn't increase, let us imagine 0,1 then as a box, when it increases, you don't get more boxes, you simply throw things in the box, is this correct? So inv[0,1] = 10 means that index is equal to 10 until i change the value and print inv[0,1] would give me 10 correct? So inventory[0,1] = 10 means you have 10 swords? Still correct? If I wanted to add this info from the 2d array to a listbox for a text base kind of inventory, i am not getting what the setup would be... How would I check for the item in the box, and if it is there, 'stack' the quantity number, instead of adding a duplicate entry to illustrate you have more than one of the same item? If item you are buying = an item in your inventory quantity of itemname = quantity of item name + 1 little bit stumped here... |
| ||
Use a TMap. Typed straight into the replybox so probably won't work right off, but you get the idea.Type tInventory Field items:TMap Method New() Self.items = New TMap End Method Method AddItem(name:String, qty:Int) Local t:tInventoryItem 'do we already have some of this item? t = tInventoryItem(Self.items.ValueForKey(name)) If t t.qty:+qty Else t = new tInventoryItem t.name = name t.qty = qty Self.items.Insert(t.name,t) EndIf End Method End Type Type tInventoryItem Field name:String Field qty:Int End Type |
| ||
thanks GFK... I never used tmaps either, what else are they useful for? I am easily understanding your example and running through the Tmap module. Understanding made easy by your powerful and simple example. One puzzle...am I intended to leave out the arrays? or just use the tmaps for checking if the object exists? this wouldn't present issues with adding to a listbox would it? |
| ||
No. Its exactly the same data, only better structured and easier to manage. |
| ||
sorry i edited mine. so no bothering with arrays. |
| ||
would it be better to use a function for create and make each item an actual object? |
| ||
If this thread is a pure inventory question the nature of the "container" (TList, TMap, array etc.) does not matter that much for what goes in to it. String/Object-reference etc. But to make the items actual objects makes sense if you want the items to behave somewhat complex in the game (like in an RPG for instance) EDIT: An interesting part of this is that you can make items, such as "bags", that actually take payloads of other items. |