Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Day 14:


I implemented the Use Item button for the inventory. Currently all items have a RespondToUseItemButton function that is called when this button is clicked, and this can alter any values in the player data - in this case, healing the player's health. I realized tonight that right now I can't efficiently change the actual inventory list from the Use Item function. Ideally I should decrease the quantity of the item (and update the inventory UI accordingly) but currently I am using an array to store the inventory items, and so when an inventory item drops to quantity 0 and /should/ be deleted, I end up with a gaping hole in the inventory list since the array does not resize. I will look into alternate data structures later - Dictionaries and Lists are options, but if I use those I will need to update Editor Scripts so that I can visualize what is in the inventory at all times.

(+1)

Generally speaking, if you need to access a list by value, dictionaries are a good candidate for consideration. In this case, since you have an arbitrary list of objects associated with a quantity, the first idea that comes to mind would be a dictionary where the keys are item IDs (perhaps constants the user never sees) and the values are quantities. I don't know if there's an idiomatic way to do this in Unity and/or C#, but as far as data design, it's the first thing that occurs to me.

It's worth the brief pain of updating the visualization scripts because it's far less likely you'll need to do that again, at least in terms of supporting the data structure.